Python: How to Algin a String (Left, Right, and Center)

Updated: June 2, 2023 By: Khue Post a comment

This pithy tutorial will show you a couple of different ways to align left, align right, or center a string in Python.

Text width & length

Before getting started, let me clarify some things. In Python, the width of a string is not necessarily the same as its length:

1. The length of a string is the number of characters it contains, which can be obtained by using the len() function.

2. The width of a string is the number of columns it occupies when displayed on a terminal or a screen. The width of a string depends on the encoding and the font used to render it. For example, some characters may take more than one column, such as Chinese characters or emoji, while some characters may take less than one column, such as combining diacritical marks.

You can measure the visual width of a string by using the wcwidth module:

pip install wcwidth

Usage:

from wcwidth import wcswidth

text = "Sling Academy"
print(wcswidth(text))  # 13

Using the str.rjust(), str.ljust(), and str.center() methods

The string ljust(), rjust(), and center() methods are used to align strings in Python. They are useful for formatting strings in a fixed-width format or aligning text in tabular structures.

1. ljust(width[, fillchar]): Left-aligns the string by adding spaces or a specified fill character (fillchar) to the right of the string until it reaches the specified width.

Example:

text = "Sling Academy"

# align left with spaces to the right
aligned_text = text.ljust(30)
print(f"'{aligned_text}'")

# align left with dashes to the right
aligned_text = text.ljust(30, "-")
print(f"'{aligned_text}'")

Output:

'Sling Academy                 '
'Sling Academy-----------------'

2. rjust(width[, fillchar]): Right-aligns the string by adding spaces or a specified fill character (fillchar) to the left of the string until it reaches the specified width.

Example:

text = "Roses are red"

# Right align text with the width of 30
aligned_text = text.rjust(30)
print(aligned_text)

# Right align text with the width of 30 and fill the remaining space with *
aligned_text = text.rjust(30, "*")
print(aligned_text)

Output:

                 Roses are red
*****************Roses are red

3. center(width[, fillchar]): Centers the string by adding spaces or a specified fill character (fillchar) on both sides of the string until it reaches the specified width.

Example:

text = "Turtle"

# center text in 15 characters, fill with spaces
aligned_text = text.center(15)
print(aligned_text)

# center text in 15 characters, fill with *
aligned_text = text.center(15, "*")
print(aligned_text)

Output:

     Turtle    
*****Turtle****

In all these methods, width is the desired width of the resulting aligned string. If the width is smaller than the length of the original string, the original string is returned as is. If fillchar is provided, it is used as the fill character instead of spaces.

Using f-strings

To align a string using f-strings in Python, you can make use of format specifiers and alignment options. Here’s how you can achieve left, right, and center alignment:

1. Left alignment: Use the < character followed by the desired width:

text = "Tortoise"

# Left alignment with 15 characters, padding with spaces
aligned_text = f"{text:<15}"
print(f"'{aligned_text}'")

# Left alignment with 15 characters, padding with underscores
aligned_text = f"{text:_<15}"
print(aligned_text)

Output:

'Tortoise       '
Tortoise_______

I wrap the first aligned string with a pair of single quotes to make the spaces visible.

2. Right alignment: Use the > character followed by the desired width:

text = "Honolulu"

# Right aligned with the width of 12, filled with spaces to the left
aligned_text = f"{text:>12}"
print(aligned_text)

# Right aligned with the width of 12, filled with dots to the left
aligned_text = f"{text:.>12}"
print(aligned_text)

Output:

    Honolulu
....Honolulu

3. Center alignment: Use the ^ character followed by the desired width.

Example:

text = "The Sun"

# Center text with the width of 20, padding with spaces
aligned_text = f"{text:^20}"
print(aligned_text)

# Center text with the width of 20, padding with dashes
aligned_text = f"{text:-^20}"
print(aligned_text)

Output:

      The Sun       
------The Sun-------

In each case, the text is placed within the curly braces {} of the f-string. The alignment option and width specifier are added after the colon :. The number following the alignment option represents the desired width of the resulting aligned string.

These f-string alignment options provide a concise way to align strings and can be especially useful when combining variables and text in a formatted output.