The center() method creates and returns a new string which is padded with the specified character.
Syntax:
string.center(length[, fillchar]) Parameters: length: length of the string after padding with the characters. fillchar:(optional) characters which needs to be padded. If it's not provided, space is taken as default argument. Returns: returns a string padded with specified fillchar and it doesn't modify the original string.
CODE 1
# Python program to illustrate # string center() in python string = "geeks for geeks" new_string = string.center(24) # here filchar not provided so takes space by default. print "After padding String is: ", new_string |
chevron_right
filter_none
Output:
After padding String is: geeks for geeks
CODE 2
# Python program to illustrate # string center() in python string = "geeks for geeks" new_string = string.center(24, '*') # here fillchar is provided print "After padding String is:", new_string |
chevron_right
filter_none
Output:
After padding String is: ****geeks for geeks*****
Recommended Posts:
- Python String | ljust(), rjust(), center()
- Program to print a doormat pattern having a string written in the center in Python
- Python String Methods | Set 2 (len, count, center, ljust, rjust, isalpha, isalnum, isspace & join)
- Python | Pandas Series.str.center()
- numpy.defchararray.center() in Python
- Mahotas – Center of Mass of given Image
- wxPython - Set window in center of screen
- PyQt5 QSpinBox - Checking if value is at center
- Mahotas - Making Image Wavelet Center
- PyQt5 QSpinBox - Making value to show in Center
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Check if given string can be formed by concatenating string elements of list
- Python | Merge Tuple String List values to String
- Python - Remove front K characters from each string in String List
- Python - Length of shortest string in string list
- Python | Delimited String List to String Matrix
- Python | Sorting string using order defined by another string
- Python | Check if string ends with any string in given list
- Mahotas - Removing border effect from Wavelet Center Image
- PyQt5 - How to make text center align for non-editable ComboBox
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

