Specify it before the alignment character (> or <), without apostrophes.

contents = {"Introduction": 1, "Python Basics": 5, "Creating Your First Program": 12, "Operators and Variables": 29}
for chapt, page in contents.items():
    print(f"{chapt:.<30}{page:.>5}")

Outputs:

Introduction......................1
Python Basics.....................5
Creating Your First Program......12
Operators and Variables..........29

From the documentation

If a valid align value is specified, it can be preceded by a fill character that can be any character and defaults to a space if omitted. It is not possible to use a literal curly brace (β€œ{” or β€œ}”) as the fill character in a formatted string literal or when using the str.format() method. However, it is possible to insert a curly brace with a nested replacement field. This limitation doesn’t affect the format() function

Answer from pho on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python β€Ί string-alignment-in-python-f-string
String Alignment in Python f-string - GeeksforGeeks
July 15, 2025 - Explanation: The first print statement ... 15 spaces, filling the remaining space with *. The third line right-aligns "Python" within 15 spaces, filling the left side with =....
Discussions

f-strings in Python 3.6 are awesome
I feel like you should have given some examples. For us who are not familiar with f-strings this post doesn't really tell us much. More on reddit.com
🌐 r/Python
233
253
March 31, 2017
f-string alignment
It works for me, mate. Maybe your computer is broken. >>> print(f"{'today':25}" + "xx") today xx >>> print(f"{'tomorrow':25}" + "xx") tomorrow xx >>> print(f"{'the day after tomorrow':25}" + "xx") the day after tomorrow xx Actually, looking at it a bit it's pretty clear that your browser is not using a monospace font. More on reddit.com
🌐 r/learnpython
6
1
April 29, 2023
Is there a better way to use f-strings to add a dollar sign to a right-aligned column of floats where all the decimal places must line up correctly?

Yuck! Mixing f-strings and comma-delimited args to print() is pretty unreadable, plus hard to get right with the extra spaces between arguments. Stick to one f-string per print().

The obvious way to do this (f"${...}") doesn't work the way you want. Run this code:

# make some test data
data = (("alpha", 1.23), ("beta", 12.34),
        ("gamma", 123.45), ("delta", 1234.56))
    
for (text, val) in data:
    text_str = f"{text}:"
    print(f"{text_str:<10}${val:>10.2f}")
#                         ^^^^^^^^^^^^^    print $ plus value, value right justified

This prints:

alpha:    $      1.23
beta:     $     12.34
gamma:    $    123.45
delta:    $   1234.56

Which does right-justify the numeric values, but the $ is in a column of its own.

The way to fix that is use the same trick the code above uses to handle the preceding text on each line. My code has to add the trailing : to each line text. It does that by creating a string with the text and an added final :. Then justify the whole string in the final f-string field. Like this:

# make some test data
data = (("alpha", 1.23), ("beta", 12.34),
        ("gamma", 123.45), ("delta", 1234.56))
    
for (text, val) in data:
    text_str = f"{text}:"
    val_str = f"${val:.2f}"
    print(f"{text_str:<10} {val_str:>10s}")
#                          ^^^^^^^^^^^^^^    print right justified *string*

That gives you what you want.

alpha:          $1.23
beta:          $12.34
gamma:        $123.45
delta:       $1234.56

Your code won't need that trick for the initial line text, of course.

More on reddit.com
🌐 r/learnpython
8
5
September 12, 2024
How to align text centered on a character?
You can use the string formatting mini-language in an f-string or .format() string method. It's possible to left or right justify values in a given field width, along with many other neat manipulations. Here's a simple example showing how, try running it: # make up some dummy data: (label, value) data = (("longer name", 42), ("short", 3.14159), ("really really long", -5.1), ) label_width = 20 # must be at least width of longest label for (label, value) in data: print(f"{label:>{label_width}s}: {value}") Edit: grammar. More on reddit.com
🌐 r/learnpython
6
5
August 1, 2024
🌐
Medium
thetechplatform.medium.com β€Ί fstring-in-python-the-complete-guide-to-fstring-formatting-900676a768d5
Fstring in Python: The Complete Guide to Fstring Formatting | by The Tech Platform | Medium
February 12, 2024 - Here are the common symbols for string alignment and padding in Fstring: <: Left-align the string within the specified width. ^: Center-align the string within the specified width. >: Right-align the string within the specified width.
🌐
Fstring
fstring.help
fstring.help: Python f-string guide
By default, values are formatted to take up only as many characters as needed to represent the content. It is however also possible to define that a value should be padded to a specific length. ... When using center alignment, where the length of the string leads to an uneven split of the padding characters, the extra character will be placed on the right ...
🌐
W3Schools
w3schools.com β€Ί python β€Ί python_string_formatting.asp
Python String Formatting
Before Python 3.6 we had to use the format() method. ... F-string allows you to format selected parts of a string.
🌐
OneUptime
oneuptime.com β€Ί home β€Ί blog β€Ί how to use f-strings for string formatting in python
How to Use f-strings for String Formatting in Python
January 25, 2026 - text = "Python" # Minimum width with alignment print(f"|{text:10}|") # |Python | (left, default for strings) print(f"|{text:<10}|") # |Python | (left) print(f"|{text:>10}|") # | Python| (right) print(f"|{text:^10}|") # | Python | (center) # Custom fill character print(f"|{text:*<10}|") # |Python****| print(f"|{text:*>10}|") # |****Python| print(f"|{text:*^10}|") # |**Python**| # Numbers are right-aligned by default num = 42 print(f"|{num:10}|") # | 42| print(f"|{num:<10}|") # |42 |
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com β€Ί article β€Ί how-to-align-text-strings-using-python
How to Align Text Strings using Python?
March 26, 2026 - Python provides multiple ways to align text: f-strings with alignment specifiers and built-in string methods like ljust(), rjust(), and center(). Python's text alignment feature helps create well-formatted output. When data varies in length, proper alignment makes output more readable and professional-looking. You can specify left, right, or center alignment with a reserved width for consistent formatting.
🌐
Medium
medium.com β€Ί @pivajr β€Ί pythonic-tips-how-to-align-strings-to-the-left-right-and-center-in-python-557db734cce6
Pythonic Tips: How to Align Strings to the Left, Right, and Center in Python | by Dilermando Piva Junior | Medium
May 22, 2025 - Here, ljust(20) ensures the product name is left-aligned within a 20-character column, while rjust(20) pushes monetary values to the right, creating a table-like illusion in terminal interfaces. The center() method is perfect for headers and visual dividers. Introduced in Python 3.6, f-strings bring poetic elegance to text formatting.
🌐
DataCamp
datacamp.com β€Ί tutorial β€Ί python-f-string
Python f-string: A Complete Guide | DataCamp
December 3, 2024 - We can align text to the left, right, or center using the '<', '>', and '^' operators respectively: # Basic text alignment name = "Python" width = 20 print(f"Left aligned: |{name:<20}|") print(f"Right aligned: |{name:>20}|") print(f"Center aligned: |{name:^20}|") Output Β·
🌐
YouTube
youtube.com β€Ί shorts β€Ί elzAmldcR_g
How To Align Text Using F-Strings - YouTube
In this python tutorial, I show you how to align text using f-strings in python! Let's get coding!======== Ask Case Digital ========If you have a question yo...
Published: December 3, 2024
🌐
YouTube
youtube.com β€Ί watch
29. String Alignment using f-string and ljust(), rjust(), center() - YouTube
#python #coding #ai #datascience Learn Python by Doing: Hands-on ExercisesStruggling with Python assignments? πŸ€” Feeling lost in your Python coursework? 🀯 S...
Published: January 29, 2024
🌐
DataCamp
datacamp.com β€Ί community β€Ί tutorials β€Ί f-string-formatting-in-python
f-string Formatting in Python Tutorial | DataCamp
August 1, 2019 - String formatting is attractively designing your string using formatting techniques provided by the particular programming language. We have different string formatting techniques in Python.
🌐
Python
docs.python.org β€Ί 3.2 β€Ί library β€Ί string.html
6.1. string β€” Common string operations β€” Python v3.2.6 documentation
October 12, 2014 - The fill character can be any character other than β€˜{β€˜ or β€˜}’. The presence of a fill character is signaled by the character following it, which must be one of the alignment options.
🌐
Reddit
reddit.com β€Ί r/python β€Ί f-strings in python 3.6 are awesome
r/Python on Reddit: f-strings in Python 3.6 are awesome
March 31, 2017 -

When f-strings were announced I was dubious - .format() is pretty neat, why bother with anything else?

Turns out they are awesome. There's not really much to say beyond that - they don't magically do things that .format() doesn't. But once you start using them, you get hooked. It just feels so surprisingly natural, you wonder why you ever did it any other way. If you don't believe me, just try it!

Kudos too to PyCharm for the syntax highlighting and autocompletion support - hope every editor picks up support soon (Jupyter notebooks, I'm looking at you!).

🌐
GeeksforGeeks
geeksforgeeks.org β€Ί python-string-ljust-rjust-center
Python String - ljust(), rjust(), center() - GeeksforGeeks
January 2, 2025 - If length is less than or equal to the length of the origin ... The rjust() method in Python is a string method used to right-align a string within a specified width by padding it with a specified character (default is a space).