Python String center() Method – Tutorial with Examples

Python String center() Method

The center() method is a built-in method in Python that returns a centered version of the string. This means that the string is padded with a specified character on both sides so that it is centered within a specified width. This method is particularly useful for formatting output in a visually appealing manner.

Syntax

The syntax of the center() method is as follows:

string.center(width, fillchar)

Here, string is the string to be centered, width is the total width of the resulting string, and fillchar is the character used to pad the string (defaults to a space if not specified).

Return Value

The center() method returns a centered version of the string with a total width of width. If the specified width is less than the length of the original string, the method returns the original string unchanged.

Examples

Here are three different examples of how to use the center() method in Python:

Example 1: Centering a String with a Space Padding

The following example demonstrates how to use the center() method to center a string with a space padding:

string = "Hello, world!"
centered_string = string.center(20)
print(centered_string)

Output:

   Hello, world!   

In this example, we center the string “Hello, world!” within a total width of 20 characters using the center() method. The resulting string is padded with spaces on both sides so that it is centered.

Example 2: Centering a String with a Custom Padding Character

The following example demonstrates how to use the center() method to center a string with a custom padding character:

string = "Hello, world!"
centered_string = string.center(20, "-")
print(centered_string)

Output:

---Hello, world!---

In this example, we center the string “Hello, world!” within a total width of 20 characters using the center() method. The resulting string is padded with hyphens on both sides so that it is centered.

Example 3: Centering a List of Strings

The following example demonstrates how to use the center() method to center a list of strings:

string_list = ["Hello, world!", "How are you?", "I'm doing well", "Guten Tag"]
max_width = max([len(string) for string in string_list])
for string in string_list:
    centered_string = string.center(max_width)
    print(centered_string)

Output:

   Hello, world!   
   How are you?    
  I'm doing well   
     Guten Tag     

In this example, we center each string in a list of strings so that they are all centered within the same width. The width is determined by the length of the longest string in the list. The resulting strings are printed out one at a time.

Use Cases

The center() method can be useful in a variety of scenarios. Here are a few examples:

  • Formatting text output in a visually appealing manner.
  • Creating table-like structures where columns are aligned.
  • Creating ASCII art.
  • Padding text for consistent column widths in data files.

Overall, the center() method is a versatile tool for formatting text output in a visually appealing way.

Leave a Reply

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