How to align text strings with Python

How to Align Text Strings in Python

In this article, we will learn how to align text strings in Python. We will use f-strings to align text strings in Python.

Python’s text alignment features are helpful for printing well-formatted, neat output. Sometimes, the length of the data being printed varies, causing it to appear uneven when printed. You can use String Alignment.NET to align the output string by specifying the alignment as left, right, or center, as well as the space (width) reserved for the string.

The text will be formatted using f-strings.NET . The alignment of the output string is defined by the symbols **’ <', '>‘, ‘^ **’, followed by a width number.

The string methods ljust(), rjust(), and center() can also be used to align strings.

Using Methods

  • Using f-strings.
  • Using ljust(), rjust(), and center() methods

Method 1: Left-Justifying with f-strings

To left-justify the output string, specify the syntax ‘ **< ' **, followed by a width number.

Example

The following program aligns text on the left with 30 spaces:

# 30 spaces are reserved in this case for the specific output string.
# On the left side, the string is printed.
print(f"{'The Left Aligned String!!!' : <30}")

Output

When executed, the above program will produce the following output:

The Left Aligned String!!!

Method 2: Right Alignment Using f-strings

Specify ‘>’ followed by a width number to right-align the output string syntax.

Example

The following program right-aligns text with 30 spaces:

# 30 spaces are reserved in this case for the specific output string.
# On the right side, the string is printed.
print(f"{'The Right Aligned String!!!' : >30}")

Output

When executed, the above program will produce the following output: -.

The Right Aligned String!!!

Method 3: Center Alignment Using f-Strings

Specify ‘^‘ followed by a width to center the output string.

Example

The following program centers text –

# 30 spaces are reserved in this case for the specific output string.
# In the middle, the string is printed.
print(f"{'Center Aligned String!!!' : ^30}")

Output

When executed, the above program will produce the following output:

 Center Aligned String!!! 

Method 4: Printing Variables in Aligned Format

Example

The following program uses f-strings and the <,^,> notation to align three given strings in three different alignment formats.

# Input string for all three types of alignments
leftAlign = "Left Alignement"
centerAlign = "Center Alignement"
rightAlign = "Right Alignement"

# Print the resultant aligned text using the <,^, > symbols
print(f"{leftAlign : <25}{centerAlign : ^20}{rightAlign : >25}")

Output

When executed, the above program will produce the following output:

Left Alignement Center Alignement Right Alignment

Method 5: Printing Multiple List Values in Aligned Columnar Format

Example

The following program uses f-strings and the <,^,> notation to align four given lists in four different columnar alignment formats.

# input lists of multiple columns
cricketers = ['Dhoni', 'Virat Kohli',
               'Hardik Pandya', 'KL Rahul', 'Rohit Sharma']
score = [55, 90, 60, 45, 70]
place = ['Ranchi', 'Mumbai', 'Mumbai', 'Mumbai', 'Kolkata']
strikerate = [90, 60, 30, 50, 70]

# Aligning the Headers and printing them
print(f"{'Cricketers' : <20}{'Score' : ^20}{'Place' : ^20}{'Strikerate' : >10}")

# Aligning the variable values and printing them
# Looping 4 times as 4 columns are used using the for loop
for i in range(0, 4):
# aligning variable values using left, center, and right alignments
# with specific widths
print(
f"{cricketers[i] : <20}{score[i] : ^20}{place[i] : ^20}{strikerate[i] : >10}")

Output

Upon execution, the above program will produce the following output

Cricketers Score Place Strikerate
Dhoni 55 Ranchi 90
Virat Kohli 90 Mumbai 60
Hardik Pandya 60 Mumbai 30
KL Rahul 45 Mumbai 50

Method 6: Aligning Text Using the ljust(), rjust(), and center() Methods

The ljust(), rjust(), and center() methods of strings can also be used to align strings.

ljust() Method

Left-aligns a string using the specified character (default: space) as the padding character.

Syntax

string.ljust(length, character)

Parameters

  • length(required) – Returns the length of the string.
  • character(optional) – The character to fill missing spaces on the right side of the string. Space (“”) is the default.

rjust() Method

Right-aligns a string using a specified character (space by default) as the padding character.

Syntax

string.rjust(length, character)

Parameters

  • length(required) – Returns the length of the string.
  • character(optional) – The character to fill any missing spaces on the left side of the string. Space (“”) is the default.

center() Method

Center aligns a string using a specified character (space by default) as the padding character.

Syntax

string.center(length, character)

Parameters

  • length(required) – Returns the length of the string.
  • character(optional) – Fills missing spaces on either side with a specified character. Spaces (“”) are the default.

Example

The following program demonstrates how to use the ljust(), rjust(), and center() methods to left-align, right-align, and center a text string, respectively.

# input text
inputText = 'Tutorialspoint'

# left aligning the text of a width of 30
print("Left Alignment:", inputText.ljust(30))

# right aligning the text of a width of 30
print("Right Alignment:", inputText.rjust(30))

# center aligning the text of a width of 30
print("Center Alignment:", inputText.center(30))

Output

When executed, the above program will produce the following output:

Left Alignment: Tutorialspoint
Right Alignment: Tutorialspoint
Center Alignment: Tutorialspoint

Method 7: Align Text with a Specified Character

Example

The following program shows how to use the ljust(), rjust(), and center() methods to left-align, right-align, and center text, respectively, using a specified character.

# input text
inputText = 'Tutorialspoint'

# left aligning the text of a width of 30
# fills the space with the '@' character
print("Left Alignment:", inputText.ljust(30, '@'))

# right aligning the text of a width of 30
# fills the space with the '#' character
print("Right Alignment:", inputText.rjust(30, '#'))

# center aligning the text of a width of 30
# fills the space with the '%' character
print("Center Alignment:", inputText.center(30, '%'))

Output

While executing, the above program will produce the following output:

Left Alignment: Tutorialspoint@@@@@@@@@@@@@@@@
Right Alignment: ################Tutorialspoint
Center Alignment: %%%%%%%%%Tutorialspoint%%%%%%%%

Summary

This article demonstrated how to use the ljust(), rjust(), and center() methods to align text strings in Python. Additionally, we learned how to use f-strings to align text strings at different positions.

Leave a Reply

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