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.
Left Aligned Text
Example 2 : For Right Alignment output string syntax define ‘>’ followed by the width number.
Right Aligned Text
Example 3 : For Center Alignment output string syntax define ‘^’ followed by the width number.
Example 4 : Printing variables in Aligned format
Left Text Centered Text Right Text
Example 5 : Printing out multiple list values in aligned column look.
Name Marks Division ID Raj 7 A 21 Shivam 9 A 52 Shreeya 8 C 27 Kartik 5 B 38
- Qt Alignment in PyQt5
- PyQt5 - alignment() method for Label
- PyQt5 – Check if alignment of radio button is left to right
- PyQt5 QSpinBox - Setting alignment
- PyQt5 QSpinBox - Getting alignment
- PyQt5 QDateEdit - Setting Date Alignment
- PyQt5 QDateEdit - Getting Date Alignment
- PyQt5 QListWidget – Setting Item Alignment
- PyQt5 QListWidget – Getting Item Alignment
- PyQt5 QListWidget – Getting all Scroll Bar at given alignment
- String slicing in Python to check if a string can become empty by recursive deletion
- String slicing in Python to rotate a string
- Python | Check if a given string is binary string or not
- Python | Check if given string can be formed by concatenating string elements of list
- Python | Check if string ends with any string in given list
- Python | Sorting string using order defined by another string
- Python | Merge Tuple String List values to String
- Python | Sort each String in String list
- Python | Convert List of String List to String List
- Python - Length of shortest string in string list