Python Output Formatting
When writing programs in Python, displaying output in a clean and readable format is very important.
Output formatting helps you:
- Present data clearly
- Improve readability
- Build professional CLI applications
- Format numbers, strings, and tables
- Control spacing and alignment
Python provides multiple ways to format output efficiently.
Why Output Formatting is Important
Without formatting:
name = "John"
age = 25
print("Name:", name, "Age:", age)Output:
Name: John Age: 25With formatting, output becomes cleaner and more readable.
1. Using f-Strings (Recommended)
f-Strings are the most modern and efficient way to format output in Python.
Example
name = "John"
age = 25
print(f"My name is {name} and I am {age} years old")Output
My name is John and I am 25 years oldExpressions inside f-Strings
You can also use expressions:
a = 10
b = 5
print(f"Sum is {a + b}")Output:
Sum is 152. format() Method
The format() method is another powerful way to format strings.
Example
name = "Alice"
age = 30
print("My name is {} and I am {} years old".format(name, age))Output
My name is Alice and I am 30 years oldPositional Formatting
print("{1} is {0} years old".format(25, "John"))Output:
John is 25 years oldNamed Formatting
print("{name} is {age} years old".format(name="John", age=25))3. Old % Formatting (Legacy)
Python also supports older formatting style.
name = "John"
age = 25
print("My name is %s and I am %d years old" % (name, age))Output
My name is John and I am 25 years oldFormat Specifiers
| Symbol | Meaning |
|---|---|
| %s | String |
| %d | Integer |
| %f | Float |
4. Formatting Numbers
Floating Point Precision
pi = 3.14159265
print(f"{pi:.2f}")Output:
3.14Padding Numbers
num = 5
print(f"{num:05}")Output:
000055. Aligning Output
Left Alignment
print(f"{'Python':<10}Easy")Right Alignment
print(f"{'Python':>10}Easy")Center Alignment
print(f"{'Python':^10}")6. Formatting Tables
print(f"{'Name':<10}{'Age':<5}")
print(f"{'-'*15}")
print(f"{'John':<10}{25:<5}")
print(f"{'Alice':<10}{30:<5}")Output
Name Age
---------------
John 25
Alice 30 7. Escape Characters
Python provides escape sequences for formatting text.
| Escape | Meaning |
| \n | New line |
| \t | Tab space |
Example
print("Hello\nWorld")Output:
Hello
World8. Using sep and end Parameters
sep parameter
print("Python", "is", "awesome", sep="-")Output:
Python-is-awesomeend parameter
print("Hello", end=" ")
print("World")Output:
Hello World9. Formatting Lists
fruits = ["Apple", "Banana", "Mango"]
print(", ".join(fruits))Output:
Apple, Banana, Mango10. Formatting Dictionaries
user = {"name": "John", "age": 25}
print(f"Name: {user['name']}, Age: {user['age']}")11. Real-World Example: Invoice Format
items = [
("Pen", 2, 10),
("Book", 1, 50),
("Bag", 1, 500)
]
print(f"{'Item':<10}{'Qty':<5}{'Price':<5}")
print("-"*25)
for item, qty, price in items:
print(f"{item:<10}{qty:<5}{price:<5}")Output Formatting Best Practices
- Use f-Strings (modern and fast)
- Keep output readable
- Avoid complex formatting in loops
- Use alignment for tables
- Use formatting for user-facing output
Common Mistakes
Mixing formatting styles
Avoid mixing f-strings and % formatting.
Forgetting type conversion
print("Age: " + 25) # ErrorCorrect:
print(f"Age: {25}")Summary
Python provides multiple ways to format output including f-strings, format() method, and legacy % formatting. Among these, f-strings are the most modern, readable, and efficient.
Conclusion
Output formatting is an essential skill in Python programming. It helps developers display data in a clean and professional way, especially in reports, dashboards, and command-line applications.
Mastering formatting techniques like f-strings, alignment, and number formatting will greatly improve your Python coding quality.


0 Comments