String Alignment in Python f-string

Text Alignment in Python is useful for printing out clean formatted output. Some times the data to be printed varies in length which makes it look messy when printed. By using String Alignment the output string can be aligned by defining the alignment as left, right or center and also defining space (width) to reserve for the string.

Approach : We will be using the f-strings to format the text. The syntax of the alignment of the output string is defined by ‘<‘, ‘>’, ‘^’ and followed by the width number.

Example 1 : For Left Alignment output string syntax define ‘<‘ followed by the width number.

filter_none

edit
close

play_arrow

link
brightness_4
code

# here 20 spaces are reserved for the 
# particular output string. And the string
# is printed on the left side
print(f"{'Left Aligned Text' : <20}")

chevron_right


Output :

Left Aligned Text

Example 2 : For Right Alignment output string syntax define ‘>’ followed by the width number.



filter_none

edit
close

play_arrow

link
brightness_4
code

# here 20 spaces are reserved for the 
# particular output string. And the string
# is printed on the right side
print(f"{'Right Aligned Text' : >20}")

chevron_right


Output :

  Right Aligned Text

Example 3 : For Center Alignment output string syntax define ‘^’ followed by the width number.

filter_none

edit
close

play_arrow

link
brightness_4
code

# here 20 spaces are reserved for the 
# particular output string. And the string
# is printed in the middle
print(f"{'Centered' : ^10}")

chevron_right


Output :

 Centered 

Example 4 : Printing variables in Aligned format

filter_none

edit
close

play_arrow

link
brightness_4
code

# assigning strings to the variables
left_alignment = "Left Text"
center_alignment = "Centered Text"
right_alignment = "Right Text"
  
# printing out aligned text
print(f"{left_alignment : <20}{center_alignment : ^15}{right_alignment : >20}")

chevron_right


Output :

Left Text            Centered Text           Right Text

Example 5 : Printing out multiple list values in aligned column look.

filter_none

edit
close

play_arrow

link
brightness_4
code

# assigning list values to the variables
names = ['Raj', 'Shivam', 'Shreeya', 'Kartik']
marks = [7, 9, 8, 5]
div = ['A', 'A', 'C', 'B']
id = [21, 52, 27, 38]
  
# printing Aligned Header
print(f"{'Name' : <10}{'Marks' : ^10}{'Division' : ^10}{'ID' : >5}")
  
# printing values of variables in Aligned manner
for i in range(0, 4):
    print(f"{names[i] : <10}{marks[i] : ^10}{div[i] : ^10}{id[i] : >5}")

chevron_right


Output :

Name        Marks    Division    ID
Raj           7         A        21
Shivam        9         A        52
Shreeya       8         C        27
Kartik        5         B        38



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.