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 ... Aligns the text to the right within the specified width. ^ (Center Alignment): Centers the text within the specified width....
🌐
Reddit
reddit.com › r/learnpython › help centering multiple f strings?
r/learnpython on Reddit: Help centering multiple f strings?
October 16, 2023 -

I am trying to centre the following ( didn't want to include the whole code as its 166 lines long)

for years in range (1,5):
Payments = years * 12
FinFee = (39.99) * years
TotPrice = TotalSalesPrice + FinFee
MonPay = TotPrice / 12

FinFeeDSP = "${:,.2f}".format(FinFee)
TotPriceDSP = "${:,.2f}".format(TotPrice)
MonPayDSP = "${:,.2f}".format(MonPay)
print(f" {years:^2d} {Payments:^9d} {FinFeeDSP:^9s} {TotalSalesPriceDSP:^9s} {MonPayDSP:^9s}")
print(f"------------------------------------------------------------------------")
CurDate = datetime.datetime.now()
CurDatePlus30 = CurDate + datetime.timedelta(days = 30)
print(f"Invoice date: {CurDate.strftime('%d-%b-%y').upper()} First payment date: {CurDatePlus30.strftime('%d-%b-%y').upper()}")
print()
print()

Maybe centre isn't the right word, but I'm trying to get the f strings with equal padding/space on each side. Right now it lines up with the printed f-strings above it and I need it more in the middle.

It currently looks like this: https://imgur.com/a/hpBBcLN

I need the table on the bottom entered or indented so there is equal space on both sides

🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › how-to-align-text-strings-using-python
How to Align Text Strings using Python?
# Right align with 30 character width text = "Right Aligned String!" print(f"{text:>30}") print(f"{'Hello':>20}World") ... # Center align with 30 character width text = "Center Aligned String!" print(f"{text:^30}") print(f"{'Hello':^20}")
🌐
W3Schools
w3schools.com › python › ref_string_center.asp
Python String center() Method
Python Overview Python Built-in ... Interview Q&A Python Training ... The center() method will center align the string, using a specified character (space is default) as the fill character....
🌐
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
🌐
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.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Right-justify, Center, Left-justify Strings and Numbers in Python | note.nkmk.me
May 18, 2023 - i = 123 print(type(i)) # <class 'int'> print(str(i).rjust(8, '*')) # *****123 print(str(i).center(8, '*')) # **123*** print(str(i).ljust(8, '*')) # 123***** ... Both the built-in format() function and the format() method of str allow various formatting options. See the following article for more information. Format strings and numbers with format() in Python
🌐
Medium
medium.com › @NirantK › best-of-python3-6-f-strings-41f9154983e
Best of Python3.6 f-strings. f-strings (formatted strings) for those… | by Nirant Kasliwal | Medium
April 16, 2018 - This is much easier with Python f-strings using the colon ‘:’ operator, followed by a an alignment operator and field width value. There are atleast 3 alignment operator: < for left aligned, > for right aligned, and ^ for center 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.
🌐
Medium
medium.com › @byte-pages › pythons-magical-f-strings-a-detailed-guide-4eb0e3c475b4
Python’s Magical F-Strings — A Detailed Guide✨ | Medium
May 19, 2024 - By using a caret ^ for center alignment and a less-than sign < for left alignment, your incantations can possess the allure of symmetry or the charm of informality. Play with these magical symbols and ...
🌐
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
🌐
LernerPython
lernerpython.com › home › lernerpython blog › python f-string format specifiers: aligning and padding text
Python f-string format specifiers: Aligning and padding text - LernerPython
April 29, 2026 - Use in f-string format specifiers, with a width integer and a custom fill character, to left-align, center or right-align and pad text.
🌐
Reddit
reddit.com › r/learnpython › how to align text centered on a character?
r/learnpython on Reddit: How to align text centered on a character?
August 1, 2024 -

Hi guys,

Can anyone help me on how I can align text in a string so it is centered on the ": "?

I was thinking something like string concatenation but not sure how to align it so I would not be blocked by a character limit on the left side.

I need to create a function that outputs the following:

Toyota Prius
       Current: 12500km
  Last service: 11000km
  Next service: 21000km
   Service due: False

Toyota Prius
       Current: 12500km
  Last service: 11000km
  Next service: 21000km
   Service due: False
🌐
Learn By Example
learnbyexample.org › python-string-center-method
Python String center() Method - Learn By Example
April 20, 2020 - By default the string is padded with whitespace (ASCII space). You can modify that by specifying a fill character. # center() with '*' as a fill character S = 'Centered' x = S.center(14, '*') print(x) # Prints ***Centered*** You can achieve the same result by using format() method. # Align text center with format() S = 'Centered' x = '{:*^14}'.format(S) print(x) # Prints ***Centered***
🌐
GeeksforGeeks
geeksforgeeks.org › python › pad-or-fill-a-string-by-a-variable-in-python-using-f-string
Pad or fill a string by a variable in Python using f-string - GeeksforGeeks
July 15, 2025 - Python · s = 'GFG' char = '*' ... · If we want to center the string and add padding on both sides, we can use the ^ symbol in f-strings....
🌐
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 ...
🌐
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 ·