9.6 String Formatting
The three ways to format strings in Python -- percent-style, str.format(), and f-strings -- plus alignment, padding, number/currency/date formatting, and building log lines.
Three Ways to Format
All three produce identical output; f-strings are the recommended default for new code.
% Formatting
The oldest style, borrowed from C’s printf. %s substitutes a string, %d an integer, %f a float. Still seen in older codebases and some logging configs, but largely superseded.
>>> "Server %s is at %s%% capacity" % ("web01", 87) # %% escapes a literal %
'Server web01 is at 87% capacity'
str.format()
Replaces {} placeholders with arguments, by position or by name. More readable than %-formatting, and still common where the template string itself is loaded from a file or config.
>>> "Server {name} is at {pct}% capacity".format(name="web01", pct=87)
'Server web01 is at 87% capacity'
f-Strings
Embed expressions directly inside {} within the string literal itself, prefixed with f. The fastest and most readable option, and the recommended default for new code since Python 3.6.
>>> name, pct = "web01", 87
>>> f"Server {name} is at {pct}% capacity"
'Server web01 is at 87% capacity'
Alignment and Padding
A format spec’s <, >, and ^ characters left-align, right-align, and center-align a value within a fixed width — the basis for neatly columned console output or reports. Padding fills unused width with a character, spaces by default.
>>> f"{'Alice':<10}|{'30':<5}" # left-align, width 10 and 5
'Alice |30 '
>>> f"{7:03d}" # zero-pad an integer to width 3
'007'
Number, Currency, and Date Formatting
Format specs insert thousands separators, convert a fraction to a percentage, or apply strftime-style date codes automatically.
>>> f"{1234567:,}" # thousands separator
'1,234,567'
>>> f"{0.4567:.2%}" # percentage
'45.67%'
>>> f"${1234.5:,.2f}" # currency: precision + separator + symbol
'$1,234.50'
>>> import datetime
>>> d = datetime.datetime(2026, 7, 13, 10, 22, 5)
>>> f"{d:%Y-%m-%d %H:%M:%S}"
'2026-07-13 10:22:05'
Logging Format Example
Combining date formatting with a static prefix/suffix is exactly how most log lines are built:
>>> f"[{d:%Y-%m-%d %H:%M:%S}] INFO Service started"
'[2026-07-13 10:22:05] INFO Service started'
Quick Interview Answer
“Python has three formatting styles:
%-formatting (legacy, printf-style, error-prone with many arguments),.format()(readable, supports positional and named fields, good when the template itself is loaded as data), and f-strings (fastest, most readable, expressions evaluated inline — the default for new code in 3.6+). Format specs share the same mini-language across all of them for alignment (</>/^), padding, thousands separators, percentages, andstrftime-style date codes.”
Common Mistakes
- Using an f-string on a template string loaded from a config file or user input — f-strings evaluate arbitrary expressions at parse time, so the template itself must be a literal in the source code; use
.format()orstring.Templatewhen the template is data. - Forgetting
%%is required to output a literal%in%-style formatting, since a bare%is interpreted as a format specifier. - Mixing positional and named
.format()arguments incorrectly, or miscounting%-style positional placeholders against the tuple of values, causing aTypeError.
Add More Questions to This Guide
Know a question that should be here? Share it and help the community!
Open Google Form