How to Align Text Strings using Python?


In this article, we will learn how to align text strings using Python. We will be using f-strings for this aligning text strings in python.

Python's Text Alignment feature is helpful for printing output that is well and cleanly formatted. Sometimes the length of the data to be printed varies, making it appear untidy when printed. By specifying the alignment as left, right, or center and the space (width) to reserve for the string, the output string can be aligned using String Alignment.

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

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

Methods Used

  • Using f-strings.

  • Using ljust(), rjust(), and centre() methods

Method 1: Left Alignment using f-strings

Specify ‘<‘ followed by the width number for the Left Alignment output string syntax.

Example

The following program aligns the text on the left side with 30 spaces reserved −

# 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

On execution, the above program will generate the following output −

The Left Aligned String!!!

Method 2: Right Alignment using f-strings

Specify ‘>‘ followed by the width number for the Right Alignment output string syntax.

Example

The following program aligns the text on the right side with 30 spaces reserved −

# 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

On execution, the above program will generate the following output −

   The Right Aligned String!!!

Method 3: Center Alignment using f-strings

Specify ‘^’ followed by the width number for the Center Alignment output string syntax.

Example

The following program aligns the text in the center −

# 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

On execution, the above program will generate the following output −

   Center Aligned String!!!   

Method 4: Print Variables in an Aligned Format

Example

The following program aligns the given 3 strings in 3 different aligned formats respectively using f-strings and <,^, > symbols −

# input string for all the 3 types of alignments
leftAlign = "Left Alignement"
centerAlign = "Center Alignement"
rightAlign = "Right Alignement"

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

Output

On execution, the above program will generate the following output −

Left Alignement           Center Alignement           Right Alignement

Method 5: Print-Multiple List Values in Aligned Column Format

Example

The following program aligns the given 4 lists in 4 aligned column formats respectively using f-strings and <,^, > symbols −

# 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 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

On execution, the above program will generate 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 ljust(), rjust(), and centre() methods

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

ljust() Method

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

Syntax

string.ljust(length, character)

Parameters

  • length(required) − returned string length.

  • character(optional) − character to fill the missing space to the right of the string. The space (“ ”) is the default.

rjust() Method

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

Syntax

string.rjust(length, character)

Parameters

  • length(required) − returned string length

  • character(optional) − character to fill the missing space to the left of the string. The space (“ ”) is the default.

center() Method

The Center aligns the string using a specified character (default- space) as the fill character.

Syntax

string.center(length, character)

Parameters

  • length(required) − returned string length

  • character(optional) − fills the missing space on either side with a character specified. The space (“ ”) is the default.

Example

The following program shows the left, right, and center alignment of a text using ljust(), rjust(), and center() methods 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

On execution, the above program will generate the following output −

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

Method 7: Aligning Text with the Specified Character

Example

The following program shows the left, right, and center alignment of a text using ljust(), rjust(), and center() methods respectively with 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

On execution, the above program will generate the following output −

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

Conclusion

This article has demonstrated how to align text strings in Python using the ljust(), rjust(), and centre() methods. Additionally, we learned how to align the text strings in different positions using f strings.

Updated on: 24-Jan-2023

20K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements