Use the new-style format method instead of the old-style % operator, which doesn't have the centering functionality:

print('{:^24s}'.format("MyString"))
Answer from Błotosmętek on Stack Overflow
🌐
W3Schools
w3schools.com › python › ref_string_center.asp
Python String center() Method
Remove List Duplicates Reverse ... Interview Q&A Python Training ... The center() method will center align the string, using a specified character (space is default) as the fill character....
🌐
Programiz
programiz.com › python-programming › methods › string › center
Python String center() (With Examples)
# returns the centered padded string of length 24 new_string = sentence.center(24, '*') print(new_string) # Output: ***Python is awesome****
🌐
Tutorialspoint
tutorialspoint.com › python › string_center.htm
Python String center() Method
The python string center() method returns the string value which is centered in the specified width.
🌐
Learn By Example
learnbyexample.org › python-string-center-method
Python String center() Method - Learn By Example
April 20, 2020 - Python · Returns center-aligned string · The center() method returns center-aligned string of length width. Padding is done using the specified fillchar (default is an ASCII space).
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-string-center-method
Python String center() Method - GeeksforGeeks
April 28, 2025 - center() method in Python is a simple and efficient way to center-align strings within a given width. By default, it uses spaces to fill the extra space but can also be customized to use any character we want.
Find elsewhere
🌐
Codecademy
codecademy.com › docs › python › strings › .center()
Python | Strings | .center() | Codecademy
October 26, 2023 - The .center() string method returns a new string that has been amended with padding. The padding is allocated evenly to both sides of the string, depending on the overall length specified.
🌐
Toppr
toppr.com › guides › python-guide › references › methods-and-functions › methods › string › center › python-string-center
Python center() function | Why do we use Python String center() function? |
August 26, 2021 - Python center() function generates and returns a padded string with the provided character. The Python center() method is an inbuilt function that is used to align a string to the center by filling paddings to the left and right of the string.
🌐
Javatpoint
javatpoint.com › python-string-center-method
Python String | center() method with Examples - Javatpoint
Python Function Programs · capitalize() casefold() center(width ,fillchar) count(string,begin,end) encode() endswith(suffix ,begin=0,end=len(string)) expandtabs(tabsize = 8) find(substring ,beginIndex, endIndex) format(value) index(subsring, beginIndex, endIndex) isalnum() isalpha() isdecimal() isdigit() isidentifier() islower() isnumeric() isprintable() isupper() isspace() istitle() isupper() join(seq) ljust(width[,fillchar]) lower() lstrip() partition() replace(old,new[,count]) rfind(str,beg=0,end=len(str)) rindex(str,beg=0,end=len(str)) rjust(width,[,fillchar]) rstrip() rsplit(sep=None, ma
🌐
Note.nkmk.me
note.nkmk.me › home › python
Right-justify, Center, Left-justify Strings and Numbers in Python | note.nkmk.me
May 18, 2023 - To right-justify, center, or left-justify, use [CHARACTER][DIRECTION][STRING_LENGTH] as the format string.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python string center() method
Python String center() Method - Spark By {Examples}
May 21, 2024 - Python center() method is a built-in string method used to center-align a string within a specified width. It creates a new string with the original
🌐
AskPython
askpython.com › python › string › python-center-method
How to use the Python center() method? - AskPython
August 6, 2022 - Python String has a lot of in-built functions to manipulate and deal with strings. The string.center() method performs padding on the string with a specific character on both the sides(left and right side) of the input string in a centralized form.
🌐
Vultr Docs
docs.vultr.com › python › standard library › str › center()
Python str center() - Center Align String
December 25, 2024 - The str.center() method in Python is a built-in string operation that allows you to center a string within a specified width by padding it with a specified character (typically spaces).
🌐
theeducationmachine
theeducationmachine.com › home › python › understanding and utilizing python’s center() string method
Understanding and Utilizing Python's center() String Method - theeducationmachine
July 9, 2023 - In this example, the string “Hello, World!” is centered within a width of 20 characters. The resulting output is padded with spaces on both sides. ... You can also specify a custom fill character using the fillchar parameter. Consider the following example: text = "Python" centered_text = text.center(10, '-') print(centered_text)
🌐
Scaler
scaler.com › home › topics › center() in python
center() in Python - Scaler Topics
May 2, 2022 - The syntax for center() method in Python is as follows: str refers to the string which needs to be centered.
🌐
Jobtensor
jobtensor.com › Tutorial › Python › en › String-Methods-center
Python String center(), Definition, Syntax, Parameters, Examples | jobtensor
The center() method will center align the string, using a specified character (space is default) as the fill character.
🌐
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
🌐
LabEx
labex.io › tutorials › python-how-to-center-strings-dynamically-419439
How to center strings dynamically | LabEx
This example demonstrates how Python's built-in .center() method simplifies text alignment, making it accessible for developers of all skill levels. The .center() method is the most straightforward way to center strings in Python.
🌐
LabEx
labex.io › tutorials › python-how-to-center-strings-with-padding-in-python-419440
How to center strings with padding in Python | LabEx
## Basic string padding demonstration text = "Python" ## Left padding with spaces left_padded = text.ljust(10) print(f"Left padded: '{left_padded}'") ## Right padding with spaces right_padded = text.rjust(10) print(f"Right padded: '{right_padded}'") ## Center padding with spaces center_padded = text.center(10) print(f"Center padded: '{center_padded}'")