In this Python tutorial, I will show you what is the center function in Python string. We will also see what is its syntax, parameters, and return values. We will explore the center() method, using examples to provide a comprehensive understanding of its functionality.
A string in Python is a sequence of characters. It’s one of the primary data types in the language and is used to represent textual data.
Python, a highly versatile and widely-used programming language, offers a multitude of methods for string manipulation. One such method is center(), which is used to center-align a string by padding it with a specified character.
What is the center() method in Python string
The string center() method is a built-in Python method that allows us to “center” a string within a specific width, padding it with a specified character (the default padding character is a space).
Syntax:
Syntax of the center() function in Python string:
string.center(width[, fillchar])
Here, the string is the Python string on which the operation is going to happen.
Parameter:
- width: The total width in which the original Python string should be centered.
- fillchar (optional): The character with which the padding should be done. If not specified, it defaults to a space.
Return Values:
- The center() function of a string returns a new string which is a padded version of the original Python string.
- If the width specified is less than or equal to the length of the original Python string, then the Python original string is returned as-is.
Center function in Python string examples
Let’s dive into the functionality of the center() method in the String Python.
Example-1: The Python center() method with default fillchar
By default, if we don’t specify the fillchar argument, the Python center() method will use a whitespace (' '
) as the padding character.
For instance, Let’s take the name of a city of the USA, neatly centered within a title block in Python.
City = "New York"
centered_City = City.center(18)
print(centered_City)
The Output is: As we can see the original Python string has 8 characters and we have provided the width of 18 characters. So, the other 10 characters will be filled with whitespace as we have not provided any fillchar parameter.
' New York '
Note: I have additionally put the output in (‘ ‘) single quote to show the whitespace.
This way we can use the string center() method in Python without any fillchar parameter.
Example-2: The string center function in Python with ‘*’ fillchar
We can also use a different character for padding like, ‘*’ asterisks. For instance, we might want to display a U.S. phone number with asterisks:
phone_number = "123-456-7890"
centered_number = phone_number.center(20, '*')
print(centered_number)
The output is: Here, the U.S. phone number “123-456-7890” is centered within a Python string of length 20, using asterisks as the fill character.
****123-456-7890****
This way we can use the center() string method in Python with fillchar parameter.
Example-3: The center() Method with length argument value less than original Python String length
If the specified width in the center() method is less than or equal to the length of the original Python string, the method returns the original Python string without any changes.
In financial reports or receipts, we might want to center-align currency amounts. Here’s an example using a dollar amount:
amount = "$450"
centered_less_width = amount.center(2)
centered_same_width = amount.center(4)
print(centered_less_width)
print(centered_same_width)
The output is: Here, in the output, the new string is same as the original Python string, because the original string length (4) is more or same than the length value provided (2) and (4). Thus, the new Python string returned is unchanged.
$450
$450
This way we can use the center() method in Python string with less or the same width value.
Example-4: The center function in Python string with non-string fill character
The fillchar argument must be a Python string of exactly one character. If not, a TypeError will be raised.
For instance, Say we’re writing a program in Python for a USA history quiz and we want to display the title in a way that grabs attention:
title = "US Presidents Quiz"
print(title.center(30, '*#'))
The output is: Here, the fillchar is two character (* and #)so, the code throw an error:
Traceback (most recent call last):
File "C:\Users\USER\PycharmProjects\pythonProject\TS\main.py", line 3, in <module>
print(title.center(30, '*#'))
^^^^^^^^^^^^^^^^^^^^^^
TypeError: The fill character must be exactly one character long
Note: To handle this error we can use the try/except block of the Python.
Conclusion
The center function is a versatile and handy tool for formatting and aligning strings in Python. With its simple syntax, parameters and diverse use cases, it plays a valuable role in enhancing the readability of text in various contexts. By understanding its functionality and using examples, we can employ this method to create more visually appealing and user-friendly applications.
You may also like to read:
- Count function in Python string
- Capitalize function in Python string
- Casefold function in Python string
- Expandtabs method in Python string
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.