You are currently viewing Python String center() Method

Python center() method is a built-in string method used to center-align a string within a specified width. It creates a new string with the original content surrounded by spaces (or any specified character) on both sides so that the total length of the new string matches the provided width.

Advertisements

In this article, I will explain the Python string center() method and using its syntax, parameters, and usage how we can center-align the given string within a specified width, using a specified fill character (space is the default) to fill the extra space on the sides with examples.

1. Quick Examples of String Center() Method

If you are in a hurry, below are some quick examples of the string center() method.


# Quick examples of string center() method

# Initialize the string
string = "Welcome to sparkbyexamples"

# Example 1: Using string center() function 
# With default fillchar
width = 40
centered_string = string.center(width)

# Example 2: Using string center() function
width = 40
fill_char = '*'
centered_string = string.center(width, fill_char)

# Example 3: Using string center() With ‘.’ as fillchar
centered_string = string.center(34, '.')

# Example 5: Use the center() function 
# In If-Else statement
if string == "Welcome to sparkbyexample":
   string1 = string.center(30, '#')
else:
   string1 = string.center(30, '!')

# Example 6: Using the center() function 
# With a smaller width
width = 10
centered_string = string.center(width, '-')

2. Python String center() Method

The string center() method centers the string within a specified width and it pads the original string with a specified character(default is space) on both sides in order to achieve the desired width. The length of the width must be greater than or equal to the original string. If its length of width is less than the original string, it returns an original string as it is.

In the first example, the center() method centers the string “Python” within a width of 10 characters, and it uses spaces as padding on both sides. In the second example, it centers the string with ‘*’ characters as padding.

2.1 Syntax of String center() Method

Following is the syntax of the string center() method.


# Syntax of string center() function
string.center(width, fillchar)

2.2 Parameter of String center() method

  • string – This is the string you want to center-align.
  • width – This is an integer representing the total width of the centered string, including the original string and any padding characters.
  • fillchar – A character that is used to fill the extra space on the left and right sides of the original string. If not provided, a space ‘ ‘ is used as the default fill character.

2.3 Return Value

It returns a new string padded with the specified fill character and does not modify the original string.

3. Usage of Python String center() Method

You can use the center() method to center align the given string with a specified width. For example, apply this method over the given string with a width of 40 characters and no fillchar parameter. It will center the alignment of the string with a specified width and by default padded with spaces on both sides to achieve the desired width.


# Initialize the string
string = "Welcome to sparkbyexamples"
print("Original string:", string)

# Using string center() function 
# With default fillchar
width = 40
centered_string = string.center(width)
print("The center alignment of the string:", centered_string)

Yields below output. The center alignment of the string.

python string center

4. Use Python String Center() with a Filling Character

You can use the center() method along with fillchar parameter to center-align a string with specified width. Set width as 40 and fillchar as asterisks(*) and pass them into center() method. It will return a new string "*******Welcome to sparkbyexamples*******"that is centered within the specified width of 40 characters and padded with the specified fill character of asterisks (*) on both sides to achieve the desired width.


# Initialize the string
string = "Welcome to sparkbyexamples"
print("Original string: ", string)

# Using string center() function
width = 40
fill_char = '*'
centered_string = string.center(width, fill_char)
print("The center alignment of the string:", centered_string)

Yields below output.

python string center

5. Using String center() With ‘.’ as Fillchar

To center the alignment of a string with a specified width you can use the center() method along with a dot(.) as the fill character. In this example, apply this method to a string and get its center alignment the original string "Welcome to sparkbyexamples" within a width of 34 characters and pad the extra space on both sides with dots(.) to achieve the desired width.


# Initialize the string
string = "Welcome to sparkbyexamples"
print("Original string: ", string)

# Using string center() With ‘.’ as fillchar
centered_string = string.center(34, '.')
print("The center alignment of the string:", centered_string)

# Output:
# Original string: Welcome to sparkbyexamples
# The center alignment of the string: ....Welcome to sparkbyexamples....

6. Use center() Function in If-Else Statement

You can use center() method with an if-else statement to center-align a string by applying different fill characters based on a condition.

In the below example, the code checks if the string is exactly equal to "Welcome to sparkbyexample"(note the missing “s” at the end). Since it’s not equal, the else part is executed, and the center() method is used to center-align the original string within a width of 30 characters and pad the extra space on both sides with exclamation marks (!).


# Initialize the string
string = "Welcome to sparkbyexamples"
print("Original string:", string)

# Use the center() function 
# In If-Else statement
if string == "Welcome to sparkbyexample":
   string1 = string.center(30, '#')
else:
   string1 = string.center(30, '!')
print("The center alignment of the string:", string1)

# Output:
# Original string: Welcome to sparkbyexamples
# The center alignment of the string: !!Welcome to sparkbyexamples!!

7. Using the center() Function with a Smaller Width

If the length of width parameter of the center() method is smaller than the length of the input string, this method doesn’t perform any padding or alignment.

Set the width as 10 and fillchar as ‘-‘ into this function, it will return the original string without any modification because its length is greater than the provided width.


# Initialize the string
string = "Welcome to sparkbyexamples"
print("Original string:", string)

# Using the center() function with a smaller width
width = 10
centered_string = string.center(width, '-')
print("The center alignment of the string:", centered_string)

# Output:
# Original string: Welcome to sparkbyexamples
# The center alignment of the string: Welcome to sparkbyexamples

8. Conclusion

In this article, I have explained the Python string center() method, and using its syntax, parameters, and usage how we can center-align the string along with the specified width and fillchar parameters with multiple examples.

Happy Learning !!