How Can We Help?
In this tutorial, we will explain how to use Python string center() method with basic syntax by example for better understanding.
How to center align string in Python? To center align string or text in Python, we use the center() method. The center method in Python returns a space padded string with the original string centered to a total of width columns.
Syntax:
<string_value/string_variable>.center(length, character)
Input Parameters:
- string_value / String variable : The string that we want to convert to the center it.
- Length : (Required) The length that we want our string will be.
- Character : (Optional) The character which we want to add it in the biggening and end of a string to reach to spicified length in the first parameter. Default is space ” ” if the parameter is not specified.
Center Align String with Fillchar in Python
Example 1
str1='python12'
print('Center:',str1.center(15,'o'))
Output of example
Center: oooopython12ooo
In the above example, the length of string variable ‘str1′ is 8, and we have increased size to 15 by adding “a” value at the start and end of the string and keeping the string value-centered using str1.center(15,’o’), so the output shows as ‘oooopython12ooo’.
Center Align String with Default Fillchar in Python
Example 2
str1='oraask'
print('Center:',str1.center(20))
Output of example
Center: oraask.com
In the above example, the length of string variable ‘str1’ is 10, and we have increased size to 20 by adding ” ” space at the start and end of the string and keeping the string value-centered using str1.center(20), so the output shows as ‘ oraask.com ‘.
In this tutorial, you have learned the string center() method in Python. What is it? And how to centralizer a given string between two padding characters.
Hopefully, it was clear and concise.
If you have an inquiry or doubt, don’t hesitate to leave them in a comment. We are waiting for your valuable feedback.
Similar Python Formatting Methods:
- title() : It returns the “title cased” version of string means all the words start with uppercase and the remaining are lowercase.
- capitalize() : It capitalizes the first character of the String.
- swapcase() : It inverts the case of all the characters in a string.
- lower() : It converts all the characters of a string to lowercase.
- upper() : It converts all the characters of a string to uppercase.
- rstrip() : It removes particular character of a string from right side.
- lstrip() : It removes particular character of a string from left side.
- strip(chars) : It removes particular characters of a string from left and right side.
- ljust() : It returns an original string with a filled specific character or space at the left side of the string value to justify the string’s new length (width).
- rjust() : It returns an original string with a filled specific character or space at the right side of the string value to justify the string’s new length (width).
- zfill() : It returns an original string value with filled zero at the left side of the string value to justify the string’s new length (width).
Related Articles
Python String – Mastering By Practical Examples (Comprehensive Guide)
Python Tutorials List:
- Python tutorials list : access all python tutorials.