Python String Formatting: % Operator and format() Method
This article explains Python's two string‑formatting approaches—the C‑style % operator and the more powerful format() method—covering integer, float, and string placeholders, alignment, precision, and advanced usage with clear code examples.
Python String Formatting
Python provides two main syntaxes for formatting output: the C‑style % operator (called Formatting Expression) and the format() method (String Formatting Method Calls). Both allow insertion of values into strings with various specifiers.
1. Integer Output
The % operator supports several integer specifiers:
Specifier
Description
Note
%o
Octal
oct
%d
Decimal
dec
%x
Hexadecimal
hex
print('%o' % 20) # octal
print('%d' % 20) # decimal
print('%x' % 24) # hexadecimal2. Float Output
Common float specifiers include:
Specifier
Description
%f
Fixed‑point with six decimal places
%e
Scientific notation with six decimal places
%g
Uses fixed‑point or scientific notation to keep six significant digits
print('%f' % 1.11) # 1.110000
print('%.1f' % 1.11) # 1.1
print('%e' % 1.11) # 1.110000e+00
print('%.3e' % 1.11) # 1.110e+00
print('%g' % 1111.1111) # 1111.11
print('%.7g' % 1111.1111) # 1111.111
print('%.2g' % 1111.1111) # 1.1e+033. String Output
String specifiers allow alignment and truncation:
Specifier
Description
%s
String
%10s
Right‑aligned in a field of width 10
%-10s
Left‑aligned in a field of width 10
%.2s
Truncate to 2 characters
%10.2s
Width 10, truncate to 2 characters
print('%s' % 'hello world') # hello world
print('%20s' % 'hello world') # hello world
print('%-20s' % 'hello world') # hello world
print('%.2s' % 'hello world') # he
print('%10.2s' % 'hello world') # he4. format() Method
The format() method uses curly braces as placeholders and offers greater flexibility.
Basic Usage
print('{} {}'.format('hello', 'world')) # hello world
print('{0} {1}'.format('hello', 'world')) # hello world
print('{0} {1} {0}'.format('hello', 'world')) # hello world hello
print('{1} {1} {0}'.format('hello', 'world')) # world world helloAdvanced Usage
Alignment specifiers:
print('{:<10s} and {:>10s}'.format('hello', 'world')) # hello and world
print('{:^10s} and {:^10s}'.format('hello', 'world')) # hello and worldPrecision and width:
print('{:.2f}'.format(1.123)) # 1.12
print('{:>10.2f}'.format(1.123)) # 1.125. Miscellaneous
Automatic Newline
for i in range(0,3):
print(i)
# Output:
0
1
2No Newline
for i in range(0,3):
print(i, end='')
# Output:
0126. Advantages of format() over %
No need to consider data types.
Single argument can be reused multiple times; order can be changed.
Highly flexible padding and alignment options.
Officially recommended; the % style is deprecated in newer Python versions.
7. Light‑hearted Closing
A short eye‑exercise reminder and a note about the FunTester public account.
FunTester
10k followers, 1k articles | completely useless
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.