This is a tutorial of the Python String center() method. Learn to add padding to a given python string with a particular character using this method.
The Python String
center() method returns a string that contains padding of a particular character at the left and the right (starting and the ending) of the given string. This method makes use of the desired length of the string to be specified by the user to add the desired character appropriate number of times at both edges of the given string.
Given below is the syntax for the string
centeredString = givenString.center(width, fillChar)
This method can take upto 2 different arguments and these are described below.
- width. This is the final length of the string to be returned including the padding characters. It should be an integer number and this argument is also mandatory to be provided.
- fillChar. This is the character to be used for the padding. It is completely optional to provide the padding character as this argument. If you won’t provide a character here, it will automatically assume the padding character to be whitespace.
center() Return Value
It returns a new string using the given string that is padded with the specified character (if fillChar is provided) or whitespace. (if fillChar is not provided)
Note. The original string remains unchanged.
Given below are the two examples demonstrating the usage of the Python String
Example 1. Applying
center() method on a string with only width argument
We have a given string and we’re applying the method
center() on it with only padding the
width argument and not providing the
fillchar argument. As we know if the
fillchar argument is not passed, it automatically assumes a space as the
fillchar argument. Hence, spaces will be used for padding the string to make the string of the length equal to the length specified as the integer number in the
givenString = "Gurmeet Singh" #Applying center() method paddedString = givenString.center(30) print("Given String: ", givenString) print("Padded String: ", paddedString) print("Given String Length: ", len(givenString)) print("Padded String Length: ", len(paddedString))
Here we’ve also used the Python built-in function
len() to calculate the length of the original and the padded string.
Given String: Gurmeet Singh Padded String: Gurmeet Singh Given String Length: 13 Padded String Length: 30
Example 2. Using Python String
center() method with both
In the following python code snippet, we’re using the
# character for adding padding to the given string.
givenString = "Gurmeet Singh" #Applying center() method #Specifying Both Arguments #width = 28, #fillchar = '#' paddedString = givenString.center(38, '#') print("Given String: ", givenString) print("Padded String: ", paddedString) print("Given String Length: ", len(givenString)) print("Padded String Length: ", len(paddedString))
Given String: Gurmeet Singh Padded String: #######Gurmeet Singh######## Given String Length: 13 Padded String Length: 28
As you can see in the output, it added the character
# 7 times at the starting of the string and 8 times at the ending of the string to achieve the specified string length of 28.
I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!