🐍1.3String Padding in Python
Mastering String Padding in Python
String padding is an essential technique in Python that helps maintain proper formatting, align text, and improve readability. Whether you're displaying data in tabular form, formatting reports, or aligning numbers, knowing how to pad strings efficiently can enhance your coding skills.
🔹 1. Using .rjust()
, .ljust()
, and .center()
Python provides built-in methods to align text within a fixed width:
text = "Python"
# Right-align with spaces (width = 10)
print(text.rjust(10)) # Output: ' Python'
# Left-align with spaces (width = 10)
print(text.ljust(10)) # Output: 'Python '
# Center-align with spaces (width = 10)
print(text.center(10)) # Output: ' Python '
✅ Custom Padding Characters
print(text.rjust(10, '-')) # Output: '----Python'
print(text.ljust(10, '*')) # Output: 'Python****'
print(text.center(10, '=')) # Output: '==Python=='
🔹 2. Padding with f-strings
Python's f-strings allow precise control over text alignment.
name = "Alice"
# Right-aligned (width=10)
print(f"{name:>10}") # Output: ' Alice'
# Left-aligned (width=10)
print(f"{name:<10}") # Output: 'Alice '
# Center-aligned (width=10)
print(f"{name:^10}") # Output: ' Alice '
✅ Using Custom Padding Characters
print(f"{name:_>10}") # Output: '_____Alice'
print(f"{name:-<10}") # Output: 'Alice-----'
print(f"{name:.^10}") # Output: '..Alice...'
🔹 3. Padding Numbers with zfill()
If you need to pad numbers with leading zeros, use .zfill()
:
num = "42"
print(num.zfill(5)) # Output: '00042'
print("-42".zfill(6)) # Output: '-00042'
📌 Note: .zfill()
works with strings. Convert integers before applying it:
num = 42
print(str(num).zfill(5)) # Output: '00042'
🔹 4. Padding Numbers with f-strings
You can also pad numbers using f-strings:
num = 42
# Right-aligned (width=6)
print(f"{num:6}") # Output: ' 42'
# Left-aligned (width=6)
print(f"{num:<6}") # Output: '42 '
# Pad with zeros (width=6)
print(f"{num:06}") # Output: '000042'
🔹 5. Padding with the .format()
Method
The .format()
method provides another way to align text:
print("{:>10}".format("Hello")) # Output: ' Hello'
print("{:<10}".format("Hello")) # Output: 'Hello '
print("{:^10}".format("Hello")) # Output: ' Hello '
print("{:_^10}".format("Hello")) # Output: '__Hello___'
🎯 Summary Table
Method | Description | Example Output |
---|---|---|
.rjust(10) |
Right-aligns text | ' Python' |
.ljust(10) |
Left-aligns text | 'Python ' |
.center(10) |
Centers text | ' Python ' |
f"{txt:>10}" |
Right-align with f-strings | ' Alice' |
f"{txt:<10}" |
Left-align with f-strings | 'Alice ' |
f"{txt:^10}" |
Center text with f-strings | ' Alice ' |
.zfill(5) |
Pad numbers with zeros | '00042' |
"{:_^10}".format(txt) |
Center with custom padding | '__Hello___' |
🚀 Conclusion
Mastering string padding in Python helps improve the readability and structure of your outputs. Whether you’re formatting tables, aligning reports, or working with numbers, these techniques will make your work more professional and polished. Try them out and take your Python skills to the next level! 🔥
Comments
Post a Comment