Methods and Functions

Python String center()

String positioning is necessary for Python for more clarity of where and how the string stands in the output environment. Python center() is a string class function that is used to position a string by padding it with a specified character. This article focuses on the Python center() function, its syntax, and its applications along with examples.

Python center() function

Definition

  • Python center() function generates and returns a padded string with the provided character.
  • The Python center() method is an inbuilt function that is used to align a string to the center by filling paddings to the left and right of the string.

center() Syntax

Python’s string center() function follows the below mentioned syntax:

                    

string.center(width, fillchar)

center() Parameters

This method accepts two parameters, the first of which is a width and the second of which is an optional fillchar. The center() function accepts 2 parameters, they are:

width – length of the string with padded characters. For example, if I have a string ‘World’ (length 5) and set the width parameter to 9, two characters will be added to the left of the string World and two characters will be added to the right, resulting in a final string length of 9.

fillchar (Optional) – character to be padded. If not provided then the default value is set to space. This value should always be a string value (specified in quotations)

Return value from center()

The Python center() function returns a string that has been padded with the provided fillchar. It makes no changes to the original string.

Example 1: center() Method with Default fillchar

Example

                    

# Python program to illustrate center()
txt = 'Hello World'
new_str = txt.center(20)
print('Centered String:', new_str)

new_str = txt.center(5)
print('Centered String:', new_str)

Output

                    

Centered String:     Hello World     
Centered String: Hello World

Explanation

In the first instance of the center() function, we have specified a width of 20 spaces. As our initial string ‘Hello World’ consists of 11 characters (whitespace included), the remaining 9 characters will be split into 4 and 5 where 4 padding spaces will be allocated to the left of the string and 5 padding spaces will be allocated to the right of the string.

In the second instance, as the width specified is less than our initial total text characters, the original string is returned without any padding or character removal.

Example 2: center() Method with fillchar

Example

                    

# Python program to illustrate center()
txt1 = 'Programming'
new_str = txt1.center(20, '*')
print('Centered String:', new_str)

txt2 = 'Welcome'
new_str = txt2.center(15, '!')
print('Centered String:', new_str)

txt3 = 'James'
new_str = txt3.center(10, '@')
print('Centered String:', new_str)

Output

                    

Centered String: ****Programming*****
Centered String: !!!!Welcome!!!!
Centered String: @@James@@@

Be careful while using center()

  • The width is an important parameter in the center() function. If you do not supply a value for it inside the center() method, an error will be triggered.

Example

                    

txt = 'Welcome'
new_str = txt.center()
print('Centered String:', new_str)

Output

                    

Traceback (most recent call last):
File "", line 2, in 
TypeError: center expected at least 1 argument, got 0

  • Because the Python string center() function has two arguments, at least one of them must be specified, and that is the width.
  • If we use several characters as a value for the fillchar parameter, we will get an error, much like if we use “#$.”

Example

                    

txt1 = 'Programming'
new_str = txt1.center(20, '***')
print('Centered String:', new_str)

Output

                    

Traceback (most recent call last):
File "", line 2, in 
TypeError: The fill character must be exactly one character long

Frequently Asked Questions

Q1. How do you center align a text in Python?

To center align a text in Python, we use the Python’s string center() function. Python center() is a string class function that is used to position a string by padding it with a specified character. The Python center() method is an inbuilt function that is used to align a string to the center by filling paddings to the left and right of the string. This method accepts two parameters, the first of which is a width and the second of which is an optional fillchar.

Example

                    

txt = 'Disneyland'
new_str = txt.center(20)
print('Centered string:', new_str)

new_str = txt.center(20, '^')
print('Centered string:', new_str)

Output

                    

Centered string:      Disneyland     
Centered string: ^^^^^Disneyland^^^^^

Q2. How do you right align a text in Python?

To right align a text in Python, we use the Python’s string rjust() function. The string rjust() method returns a right-justified string with a minimum width specified. The syntax is similar to the Python center() function. If the fillchar argument in the syntax of the function is specified, the defined character is also used to fill the remaining space.

Example

                    

txt = 'Countries'
new_str = txt.rjust(15)
print('Right-justified string:', new_str)

new_str = txt.rjust(15, '*')
print('Right-justified string:', new_str)

Output

                    

Right-justified string:       Countries
Right-justified string: ******Countries

Q3. How do you left align a text in Python?

To left align a text in Python, we use the Python’s string ljust() function. The string ljust() method returns a left-justified string with a minimum width specified. The syntax is similar to the Python center() function. If the fillchar argument in the syntax of the function is specified, the defined character is also used to fill the remaining space.

Example

                    

txt = 'Countries'
new_str = txt.ljust(15)
print('Left-justified string:', new_str)

new_str = txt.ljust(15, '*')
print('Left-justified string:', new_str)

Output

                    

Left-justified string: Countries      
Left-justified string: Countries******

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.