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 - Pythonโ€™s f-strings allow text alignment using three primary alignment specifiers: < (Left Alignment): Aligns the text to the left within the specified width. > (Right Alignment): Aligns the text to the right within the specified width.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_string_formatting.asp
Python String Formatting
F-String was introduced in Python 3.6, and is now the preferred way of formatting strings.
Discussions

python - Format output string, right alignment - Stack Overflow
I am processing a text file containing coordinates x, y, z 1 128 1298039 123388 0 2 .... every line is delimited into 3 items using words = line.split() After processing ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
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
How do I right-justify strings with dollar signs and percentages?
"Formatting doesn't seem to do literally anything at all." Well, all of Python's number formatting methods allow you to restrict the output to two decimal places and to right justify it. I suggest that you post your code so that people here can advise you what is going wrong with it. You can edit your original post to include it. Guide to posting code in this subreddit: https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_format_code.3F More on reddit.com
๐ŸŒ r/learnpython
8
3
February 13, 2022
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
Top answer
1 of 8
337

Try this approach using the newer str.format syntax. This uses a width of 12, space padded, right aligned.

line_new = '{:>12}  {:>12}  {:>12}'.format(words[0], words[1], words[2])

Example with the interpreter:

>>> line = "123 456 789"
>>> words = line.split(' ')
>>> line_new = '{:>12}  {:>12}  {:>12}'.format(words[0], words[1], words[2])
>>> print(line_new)
         123           456           789

And here's how to do it using the old % syntax (useful for older versions of Python that don't support str.format):

line_new = '%12s  %12s  %12s' % (words[0], words[1], words[2])
2 of 8
90

Here is another way how you can format using 'f-string' format:

print(
    f"{'Trades:':<15}{cnt:>10}",
    f"{'Wins:':<15}{wins:>10}",
    f"{'Losses:':<15}{losses:>10}",
    f"{'Breakeven:':<15}{evens:>10}",
    f"{'Win/Loss Ratio:':<15}{win_r:>10}",
    f"{'Mean Win:':<15}{mean_w:>10}",
    f"{'Mean Loss:':<15}{mean_l:>10}",
    f"{'Mean:':<15}{mean_trd:>10}",
    f"{'Std Dev:':<15}{sd:>10}",
    f"{'Max Loss:':<15}{max_l:>10}",
    f"{'Max Win:':<15}{max_w:>10}",
    f"{'Sharpe Ratio:':<15}{sharpe_r:>10}",
    sep="\n"
)

This will provide the following output:

Trades:              2304
Wins:                1232
Losses:              1035
Breakeven:             37
Win/Loss Ratio:      1.19
Mean Win:           0.381
Mean Loss:         -0.395
Mean:               0.026
Std Dev:             0.56
Max Loss:          -3.406
Max Win:             4.09
Sharpe Ratio:      0.7395

What you are doing here is you are saying that the first column is 15 chars long and it's left-justified and the second column (values) is 10 chars long and it's right-justified.

If you joining items from the list and you want to format space between items you can use `` and regular formatting techniques.

This example separates each number by 3 spaces. The key here is f"{'':>3}"

print(f"{'':>3}".join(str(i) for i in range(1, 11)))

output:

1   2   3   4   5   6   7   8   9   10
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-align-text-strings-using-python
How to Align Text Strings using Python?
# Left align with 30 character width text = "Left Aligned String!" print(f"{text:<30}") print(f"{'Hello':<20}World") ... # Right align with 30 character width text = "Right Aligned String!" print(f"{text:>30}") print(f"{'Hello':>20}World")
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Right-justify, Center, Left-justify Strings and Numbers in Python | note.nkmk.me
May 18, 2023 - Use the rjust() method to right-justify strings. Built-in Types - str.rjust() โ€” Python 3.11.3 documentation ยท The first argument determines the length of the output string.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
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 |
๐ŸŒ
Bentley
cissandbox.bentley.edu โ€บ sandbox โ€บ wp-content โ€บ uploads โ€บ 2022-02-10-Documentation-on-f-strings-Updated.pdf pdf
A Guide to Formatting with f-strings in Python - CIS Sandbox
There are several ways to align variables in f-strings. The various alignment options are as ... Forces the expression within the curly braces to be left-aligned. This is the ... Forces the expression within the curly braces to be right-aligned.
๐ŸŒ
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.
๐ŸŒ
USAVPS
usavps.com โ€บ home โ€บ blog โ€บ python tutorial: how to right align in python?
Python Tutorial: How to Right Align in Python? - USAVPS
March 18, 2026 - In this example, the string โ€œAliceโ€ is right aligned within a width of 10 characters, resulting in five spaces before the name. Introduced in Python 3.6, f-strings provide a more concise way to format strings.
๐ŸŒ
Sling Academy
slingacademy.com โ€บ article โ€บ python-how-to-algin-a-string
Python: How to Algin a String (Left, Right, and Center) - Sling Academy
For example, some characters may take more than one column, such as Chinese characters or emoji, while some characters may take less than one column, such as combining diacritical marks. You can measure the visual width of a string by using the wcwidth module: ... The string ljust(), rjust(), and center() methods are used to align strings in Python.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-format-strings-in-python
How to format strings in Python
The padding will start at 4. It ... is the computer science portal for {1:10}!" ... By default, strings are left-aligned within the field, and numbers are right-aligned....
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_string_rjust.asp
Python String rjust() Method
Note: In the result, there are actually 14 whitespaces to the left of the word banana. The rjust() method will right align the string, using a specified character (space is default) as the fill charac...
๐ŸŒ
Iditect
iditect.com โ€บ faq โ€บ python โ€บ format-output-string-right-alignment-in-python.html
Format output string, right alignment in python
You can format output strings with right alignment in Python using the .format() method or f-strings (available in Python 3.6 and later) with the > (greater than) symbol.
๐ŸŒ
KDnuggets
kdnuggets.com โ€บ mastering-f-strings-in-python
Mastering f-strings in Python - KDnuggets
November 7, 2024 - F-strings allow you to align text to the left, right, or center using <, >, and ^ respectively.
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ programming โ€บ formatting strings and numbers in python
Formatting strings and numbers in python | Towards Data Science
June 27, 2021 - You can use the :>, :< or :^ option in the f-format to left align, right align or center align the text that you want to format. ... After formatting the alignment of output text ...
๐ŸŒ
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 ...
๐ŸŒ
Finxter
blog.finxter.com โ€บ 5-best-ways-to-align-text-strings-using-python
5 Best Ways to Align Text Strings Using Python โ€“ Be on the Right Side of Change
February 27, 2024 - In this code snippet, the string โ€œAlignMeโ€ is aligned to the left, right, and center within a width of 10 characters, using โ€˜-โ€˜ as the padding character. These methods update the string to be printed in the format specified. Introduced in Python 3.6, f-strings offer a new way to format strings that is both concise and readable.