Python String center()

Python String center() method is a built-in function used to align the string to the center by filling the paddings to the left and right of the string with a specified fillchar(default fill character is an ASCII space). 

center() Syntax

The syntax of center() method is:

string.center(width[, fillchar])

center() Parameter

The center() function takes 2 parameters.

  • width – length of the string with padded characters
  • fillchar (optional) – Character which needs to be padded. If not provided, space is used as the default character.

 center() Return Value

The center() function returns a string padded with a specified fillchar. It doesn’t modify the original string.

The original string is returned if the width is less than or equal to len(s)

Example 1: center() Method With Default fillchar

text = "Python Rocks"

# Defaults fills with space on both sides of string
new_text = text.center(20)

print("Original String:", text)
print("Centered String:", new_text)

Output

Original String: Python Rocks
Centered String:     Python Rocks    

Example 2: center() Method With * fillchar

text = "Python Rocks"

# Defaults fills with * on both sides of string
new_text = text.center(20, "*")

print("Original String:", text)
print("Centered String:", new_text)

Output

Original String: Python Rocks
Centered String: ****Python Rocks****

Example 3: Returns original string if the width is less than length of string

In this example the width specified is 10 and the length of the string is 12. Hence the center() method returns the original string as-is without any padding characters.

text = "Python Rocks"

# width is less then length of string
new_text = text.center(10, "*")

print("Length of string:", len(text))
print("Original String:", text)
print("Centered String:", new_text)

Output

Length of string: 12
Original String: Python Rocks
Centered String: Python Rocks
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python Print To File

Python Print to File

We always use print statements in Python to display the output in the console or command line terminal. However, sometimes we want to change this behavior to print to a…
View Post
How To Rename A File In Python

How to Rename a file in Python?

Table of Contents Hide Python Rename FileExample to rename a file in PythonPython Rename Multiple FilesExample to Rename Multiple Files in Python The os module in Python comes in handy…
View Post
Python List Insert()

Python List insert()

Table of Contents Hide Syntax of List insert() insert() ParametersReturn Value from List insert()Example 1: Inserting an Element to the ListExample 2: Inserting an element at the start and end of…
View Post
How To Copy A File In Python

How to Copy a File in Python?

Table of Contents Hide Modules to copy a file in Python shutil module top copy a file in Pythoncopy() copy2() copyfile() copyfileobj()os module to copy a file in Pythonpopen() system() subprocess…
View Post