Open In App

String Alignment in Python f-string

Last Updated : 28 Jul, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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.




# 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}")


Output :

Left Aligned Text

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




# 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}")


Output :

  Right Aligned Text

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




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


Output :

 Centered 

Example 4 : Printing variables in Aligned format




# 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}")


Output :

Left Text            Centered Text           Right Text

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




# 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}")


Output :

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


Similar Reads

Face Alignment with OpenCV and Python
Face Alignment is the technique in which the image of the person is rotated according to the angle of the eyes. This technique is actually used as a part of the pipeline process in which facial detection is done using the image. This implementation of face alignment can be easily done with the help of python module cv2(computer vision). It has so m
6 min read
Qt Alignment in PyQt5
In PyQt5, Qt alignment is used to set the alignment of the widgets. In order to use the Qt alignment methods, we have to import Qt from the QtCore class. from PyQt5.QtCore import Qt There are many methods in Qt alignment :1. Qt.AlignLeft 2. Qt.AlignRight 3. Qt.AlignBottom 4. Qt.AlignTop 5. Qt.AlignCenter 6. Qt.AlignHCenter 7. Qt.AlignVCenter Note:
2 min read
PyQt5 - alignment() method for Label
In this article, we will see about alignment() method. This method is used to know the type of alignment the label is having. We use setAlignment() method to set the alignment, alignment() method returns the Qt.Alignment object which is the argument of the setAlignment() method. Syntax : label.alignment() Argument : It takes no argument. Return : I
2 min read
PyQt5 – Check if alignment of radio button is left to right
In this article we will see how to check if the alignment of radio button is left to right or not. By default alignment of radio button is left to right although with the help of setLayoutDirection we can change its alignment. Left to right direction is basically tells that indicator is on the left side and text part is on the right side. Overview
2 min read
PyQt5 QSpinBox - Setting alignment
In this article we will see how we can set alignment to the spin box, by default the spin box text is left aligned although we can change it, there are many alignment available for spin box. There are lots of alignment flags offered for spin box. Below is the list of alignment flag Qt.AlignLeft Qt.AlignRight Qt.AlignHCenter Qt.AlignJustify Qt.Align
2 min read
PyQt5 QSpinBox - Getting alignment
In this article we will see how we can get the alignment of the spin box, by default the spin box text is left aligned although we can change it, there are many alignment available for spin box. In order to get the alignment flag of the spin box we use alignment method. Syntax : spin_box.alignment() Argument : It takes no argument Return : It retur
2 min read
PyQt5 QDateEdit - Setting Date Alignment
In this article we will see how we can set the date alignment of the QDateEdit. By default the text is aligned on the left-hand side although by changing the alignment we can make the date appear in the center. Changing alignment does not effect the functionality of the date edit. In order to do this we use setAlignment method with the QDateEdit ob
1 min read
PyQt5 QDateEdit - Getting Date Alignment
In this article we will see how we can get the date alignment of the QDateEdit. By default, the text is aligned on the left hand side although by changing the alignment we can make the date appear in the center with the help of setAlignment method. Changing alignment does not effect the functionality of the date edit. In order to do this we use ali
2 min read
PyQt5 QListWidget – Setting Item Alignment
In this article we will see how we can set the item alignment of the QListWidget. QListWidget is a convenience class that provides a list view with a classic item-based interface for adding and removing items. QListWidget uses an internal model to manage each QListWidgetItem in the list. This property holds the alignment of each item in its cell. T
2 min read
PyQt5 QListWidget – Getting Item Alignment
In this article we will see how we can get the item alignment of the QListWidget. QListWidget is a convenience class that provides a list view with a classic item-based interface for adding and removing items. QListWidget uses an internal model to manage each QListWidgetItem in the list. This property holds the alignment of each item in its cell. T
2 min read
Article Tags :
Practice Tags :