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 - Each alignment specifier is followed ... left-aligns "Hello", so spaces are added to the right. The > specifier right-aligns "Hello", meaning spaces are added before it....
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 |
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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
๐ŸŒ
DEV Community
dev.to โ€บ bybydev โ€บ advanced-string-formatting-with-f-strings-in-python-3a8b
Advanced string formatting with f-strings in Python - DEV Community
May 7, 2024 - price = 123.456789 # Format to ... ${price:.0f}" print(formatted_price_int) # Output: Price (rounded): $123 ยท You can use :<w, :>w, or :^w to align a string to the left, right, or center within a given width w, where w is an ...
๐ŸŒ
YouTube
youtube.com โ€บ codelines
python f string alignment - YouTube
Download this code from https://codegive.com F-strings, introduced in Python 3.6, provide a concise and readable way to embed expressions inside string liter...
Published: December 14, 2023
Views: 9
๐ŸŒ
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.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ string โ€บ rjust
Python String rjust() (With Examples)
# right aligns 'Python' up to width 10 using '*' result = text.rjust(10, '*') print(result) # Output: ***Python ... Here, rjust() right aligns string using fillchar up to width.