The Python center() method aligns the string center with the string’s left and right padding. This function takes two parameters, the first is the width, and the second is an optional fillchar. The fillchar is a character used for padding the string to the left and right.
The syntax of the method is: str.center(width[, fillchar])
Key Points :
• This method is used in python to provide a padding on the left and right sides of the specified string (it may be a space or some other character). The method’s name is center because the given string is held in the center, and padding is provided on both sides.
• If this method is modified to a string, the original string will stay the same.
We’ll be covering the following topics in this tutorial:
Python String center(): Parameters
The center () method of Python string has two arguments, namely width, and fillchar. These arguments are listed below:
• width: Defines the final length(width) of the string of padded characters.
• fillchar: The padding character is used for this attribute, and it is an optional argument; if not provided, the default argument for that is space.
Return Value from center()
The center () string method returns a string padded with the provided character, and the main thing is that the original string is not changed.
Below is the python program to demonstrate the center() function:
# Python program to illustrate # string center() in python str = "Hi Technology Motivator"; print (str.center(40, '#'))
When we run above the program, the outcome is as follows:
Output : ########Hi Technology Motivator#########
Below are several other functions that we can use to work with string in Python 3