Text alignment positions interpolated text within a specified width, making records, columns, and other output easier to scan. Python f-strings use format specifiers to control this presentation. section 2.1.4
Use these f-string alignment components:
- `<` left-aligns text, `>` right-aligns it, and `^` centers it. section 2.1.4
- Specify width with an integer, as in `^12`; omitted padding defaults to spaces. section 2.1.4
- Choose another padding character by placing it before the alignment marker, as in `*>10` or `*^10`. section 2.1.4
Align task records
task_ids = [1, 2, 3] task_names = ['Do homework', 'Laundry', 'Pay bills'] task_urgencies = [5, 3, 4] for i in range(3): print(f'{task_ids[i]:^12}{task_names[i]:^12}{task_urgencies[i]:^12}') # Output: 1 Do homework 5 2 Laundry 3 3 Pay bills 4 section 2.1.4
Repeated format specifications can signal a DRY violation. Refactor repeated formatting into a function that accepts a format string, such as `^15` or `^18`, when output layouts vary. section 2.1.4
Figure 2.3 Improved clarity when the texts are presented in an organized structure (scenario B) compared with the default left alignment (scenario A)
Compare default left alignment with organized columns to see how spacing creates clearer visual structure. section 2.1.4

