[Python Basics] print() Formatting — A Quick Guide to Make Your Outputs Shine!


1. Percent (%) Formatting — The Old C-Style
This is the classic way of formatting strings, inspired by the C language.
name = "EJ" age = 28 print("My name is %s and I am %d years old." % (name, age))
Common format codes
%s: string
%d: integer
%f: float
You can also control decimal places:
pi = 3.14159 print("PI is %.2f" % pi) # PI is 3.14
⚠️ Although it’s still supported, this method is considered outdated.
2. format() Method — More Powerful & Flexible
Introduced in Python 2.7+, this approach offers more control and readability.
print("My name is {} and I am {} years old.".format(name, age))
Use placeholders with indexes or variable names:
print("Name: {0}, Age: {1}".format(name, age)) print("Name: {name}, Age: {age}".format(name=name, age=age))
Format floats easily:
print("PI is {:.3f}".format(pi)) # PI is 3.142
3. f-Strings — The Modern, Cleanest Way (Python 3.6+)
If you're using Python 3.6 or later, this is the go-to method. It’s concise, readable, and supports expressions directly inside the string.
print(f"My name is {name} and I am {age} years old.")
Supports inline expressions:
print(f"Next year, I’ll be {age + 1} years old.")
Formatting numbers:
print(f"PI is {pi:.2f}") # PI is 3.14
Text alignment:
print(f"|{'left':<10}|{'center':^10}|{'right':>10}|") # Output: |left | center | right|
4. Conclusion
Method | Pros | Cons |
% format | Simple, familiar in old code | Less readable, outdated |
.format() | Powerful, supports variables | Verbose, slightly clunky |
f-String | Clean, modern, readable | Requires Python 3.6+ |
While all three formatting methods are still usable, f-strings are by far the most Pythonic and preferred way to format your output today. If you're working in Python 3.6 or higher, make f-strings your default choice!
💬 Have a favorite formatting trick or want to share how you use print()
in real projects? Drop a comment below!
Subscribe to my newsletter
Read articles from EJ Jung directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
