Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Align Text Strings using Python?
In this article, we will learn how to align text strings using Python. Python provides multiple ways to align text: f-strings with alignment specifiers and built-in string methods like ljust(), rjust(), and center().
Python's text alignment feature helps create well-formatted output. When data varies in length, proper alignment makes output more readable and professional-looking. You can specify left, right, or center alignment with a reserved width for consistent formatting.
F-String Alignment Syntax
F-strings use alignment symbols followed by width numbers ?
< ? Left alignment
> ? Right alignment
^ ? Center alignment
Left Alignment using F-strings
Use < followed by the width number for left alignment ?
# Left align with 30 character width
text = "Left Aligned String!"
print(f"{text:<30}")
print(f"{'Hello':<20}World")
Left Aligned String! Hello World
Right Alignment using F-strings
Use > followed by the width number for right alignment ?
# Right align with 30 character width
text = "Right Aligned String!"
print(f"{text:>30}")
print(f"{'Hello':>20}World")
Right Aligned String!
HelloWorld
Center Alignment using F-strings
Use ^ followed by the width number for center alignment ?
# Center align with 30 character width
text = "Center Aligned String!"
print(f"{text:^30}")
print(f"{'Hello':^20}")
Center Aligned String!
Hello
Formatting Table Data
Combine different alignments to create formatted tables ?
# Creating a formatted table
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]
cities = ['NYC', 'LA', 'Chicago']
# Print header
print(f"{'Name':<12}{'Age':^8}{'City':>15}")
print("-" * 35)
# Print data rows
for name, age, city in zip(names, ages, cities):
print(f"{name:<12}{age:^8}{city:>15}")
Name Age City ----------------------------------- Alice 25 NYC Bob 30 LA Charlie 35 Chicago
Using String Methods for Alignment
ljust() Method
Left-justifies text within specified width ?
text = "Python" print(text.ljust(15)) print(text.ljust(15, '*'))
Python Python*********
rjust() Method
Right-justifies text within specified width ?
text = "Python" print(text.rjust(15)) print(text.rjust(15, '*'))
Python
*********Python
center() Method
Centers text within specified width ?
text = "Python" print(text.center(15)) print(text.center(15, '*'))
Python
****Python*****
Comparison of Methods
| Method | Syntax | Fill Character | Best For |
|---|---|---|---|
| F-string | f"{text:<10}" |
Space only | Modern Python, complex formatting |
| ljust() | text.ljust(10, '*') |
Customizable | Custom fill characters |
| rjust() | text.rjust(10, '*') |
Customizable | Custom fill characters |
| center() | text.center(10, '*') |
Customizable | Custom fill characters |
Practical Example: Invoice Formatting
# Format an invoice with proper alignment
items = [
("Laptop", 999.99),
("Mouse", 25.50),
("Keyboard", 75.00)
]
print(f"{'INVOICE':^30}")
print("=" * 30)
print(f"{'Item':<15}{'Price':>15}")
print("-" * 30)
total = 0
for item, price in items:
print(f"{item:<15}${price:>14.2f}")
total += price
print("-" * 30)
print(f"{'Total':<15}${total:>14.2f}")
INVOICE
==============================
Item Price
------------------------------
Laptop $ 999.99
Mouse $ 25.50
Keyboard $ 75.00
------------------------------
Total $ 1100.49
Conclusion
Python offers flexible text alignment through f-strings and string methods. Use f-strings for modern formatting and string methods when you need custom fill characters. Proper alignment improves output readability and creates professional-looking formatted text.
---