Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Python String center()

Python String center() method is an inbuilt method in python which is used to create and return a new string that is padded with the specified character.

  • Basically this method is used in python to provide padding with a specified character(it can be a space or it can be any other character) on both the left and the right side of the given string, hence the name of the method is center, as the given string is kept in the center and on both side, padding is provided.

  • Whenever this method is applied to a string then the original string remains the same.

Python String center(): Syntax

Below we have a basic syntax of string center() method in Python:

string.center(width, fillchar)

Python String center(): Parameters/Arguments

The Python string center() method has two arguments namely: width and fillchar.The description of these arguments is given below:

  • width:

    This attribute specifies the final length(width) of the string, with the padded characters. For example, is I have a string Hello(length 5), and I specify the width parameter to be 9, then 2 characters will be added to the left of the string Hello and two characters will be added to the right, making the length of the final string as 9.

  • fillchar:

    This attribute is used to specify padding character and it is an optional argument; in case if it is not given then space is taken as its default argument.

Python String center(): Returned Values

The Python string center() method returns a string padded with the specified character and the important thing is it doesn't modify the original string.

Python String center(): Basic Example

Below we have an example to show the working of the python string center() function:

s1 = 'hello this is a string'
s2 = s1.center(30, "*")
print(s2)

The Output will be:


****hello this is a string****

Python String center(): If Length of string is larger than the width parameter

Let us see the code snippet given below where the length of the string is more than the width that is passed as a parameter of the center() funtion:

str1 = "I am Developer"
str2 = str1.center(10, "#")
print(str2)

The Output will be:


I am Developer

The above output indicates that if the width that is passed as a parameter is less than the length of the original string in that case original string will be returned as it is after the application of center() method, because there is no need to provide extra character padding to the string to increase its length as the string already has a length larger than the given parameter.

Time for a Live Example!

Now we will show you a live example where we will use center() method in different situation and see the Output:

Important Points

  • The width is an important attribute if you do not specify it's value inside center() method, then an error will be raised.

  • As there are two arguments of python string center() function, thus it is mandatory to specify at least one of them and it is the width.

  • If multiple characters will be used as a value for fillchar argument then it will lead to an error like if we use "#$".

Summary

In this tutorial, we had learned center() method of strings in Python which is used to create and return a new string that is padded with the specified character. We saw arguments of this method with some basic and live examples.