| by Arround The Web | No comments

Python String Center() Method

In Python and other programming languages, we need to have a specified length of a string according to the requirement in any scenario. The specified length of the input string can be obtained in different ways. In Python programming, there is a built-in feature for strings that align the string in the center either by adding any symbol or without it. This article explains the working phenomenon of the center() function by including some examples.


string.center(str_length, [fill_char])

Parameters of the Function:

  • Str_length: Input length of a string that is to be added at the execution of the function.
  • Fill_char: An optional argument that adds any character to the input string to maintain the desired length.

Return string: A string that is padded with the character to fill the space up to the desired length of the output string.

Example 1: Use the Center() Function Without Any Character

The first example is declared here which is quite a simple approach to demonstrate the working of the center() method for a string data type. A five-word string is initialized to a variable. Then, this variable is used as an object to call the center function. Whereas the resultant string is stored in a new string. A special number is added as a parameter to add the padding distance to that string.

New_string = string.center(50)

The number that is used for the padding purpose is “50”. As we previously described, a fill character is provided as optional to add the padding to the input string. In this case, it is not given. So, by default, there is no specified character, only space is applied. The padding space is given on the left side and the right side by default when there is no fill character.

After applying the center() function, we display the resultant value that is stored in the new_string variable.

Save the code and then execute it. The resultant console displays the results on the Spyder tool.

You will see that the output has the string a distance apart from the left side of the console screen. The blank space is added on both sides as a default value for the padding purpose. Collectively, the space and the input string combine to make an output string of up to 50 characters.

Example 2: Use the Center() Function with a Filling Character

The second example is the one having a fill character along with the total string length that we want to create our desired output string. The filling character can be any symbol, letter, or mathematical notation. An input string is initialized to a string data type variable. This string is of length 21. Then, we apply the center() function.

New_string = string.center(44, '*')

The function parameter contains the total length of the output string and the character that we want to fill that space area to collectively make the resultant string up to 44 characters. After applying the function, the string resultant variable is subjected to the print statement.

The results show that the output string is made of both the input string and the “*” character. The padding effect applied on the input string of 21 lengths made the string come in the center. As it is the center() function, the distribution of the filling characters is made in such a way that the input string always comes in the center. The left side of the input string has 11 whereas the right side has 12 “*” characters in the output string.

Example 3: Use the Center() Function by Having a Length Parameter Smaller than the Input String Length

In this example, we will see the results when a special number that is less than the actual length of the input string is given as a parameter to the center function.

For this purpose, we have taken a string of 10-letter characters. Here, we directly use the function in the print statement. The length of the string for the output is smaller than the input length.


Execute the code to see the result.

You will see that the output is unchanged. The input string is displayed as it is. This is how the length attribute plays a role in performing the center() function.

Now, in the same example, we will increase the parameter and length of the new string. We have not provided additional filling characters in the parameter of the function to occupy the space. But the default space is utilized by default. The string length number from 5 is incremented to 25.


Upon the execution of the program, you will see that “Linuxhint”, the input string, is dragged with a particular distance towards the right. If you provided the parameter with a specified character, it would be added to the new string instead of space on both sides.

From this example, one thing is clear: the length of the output string should always be greater than the length of the input string.

Example 4: Use the Center() Function in If-Else Statement

A center() function, like other in-built functions of string, can be applied to the basic programming statements. For example, we use a conditional statement in the Python program to illustrate its working. In the if-else statement, two conditions are provided and for each condition. A consequence is also added. A check is applied to the scenario. If the condition is true, the first line of the code is executed. And on the other way, the “else” part is executed.

We initialize a string variable with two words. The “if” statement checks if the input string is equal to a supposed string, to proceed the first task which is to print the input string along with 20 length string having “#” in it.

If str == "Python programmig":

The previous statement becomes false, as the input string does not match with it because it has a missed alphabet in it.

The else part is that if the string does not match, print the string with a 20 characters string having the “!” symbol in it.

Str2 = str.center(20, '!')

After that, the input string is displayed. And the resultant string is also displayed along with the filling characters.

Now, execute the code. You will see that the new value of the string has two exclamation marks on both sides of the input string.


Python center() function is utilized to increase the length of the input string by keeping the string in the center. The function generates a new string every time, and the input string is the same. In the tutorial, we added some examples for both the types of center() functions, whether they take the filling character as a parameter or not. In the second case, a blank area is added automatically on both sides of a string. Furthermore, we come to know that the smaller length of the output string as compared to the input string is given causes no change in the string.

Share Button

Source: linuxhint.com

Leave a Reply