Python f-string format specifier has an f prefix and uses {} brackets to evaluate values. Where Format specifiers for types, padding, or aligning are specified after the colon character.
Python f-string format specifier
Simple example code string formatting in Python.
name = 'John'
age = 25
print('%s is %d years old' % (name, age))
print('{} is {} years old'.format(name, age))
print(f'{name} is {age} years old')
Output:
f-string expressions
You can put expressions between the {}
brackets.
bags = 3
apples_in_bag = 12
print(f'There are total of {bags * apples_in_bag} apples')
Output: There are a total of 36 apples\
f-string dictionaries
user = {'name': 'John Doe', 'occupation': 'gardener'} print(f"{user['name']} is a {user['occupation']}")
f-string debug
import math
x = 0.8
print(f'{math.cos(x) = }')
print(f'{math.sin(x) = }')
Multiline f-string
name = 'John Doe' age = 32 occupation = 'gardener' msg = ( f'Name: {name}\n' f'Age: {age}\n' f'Occupation: {occupation}' ) print(msg)
f-string format width
for x in range(1, 11):
print(f'{x:02} {x*x:3} {x*x*x:4}')
Output:
01 1 1
02 4 8
03 9 27
04 16 64
05 25 125
06 36 216
07 49 343
08 64 512
09 81 729
10 100 1000
f-string justify string
s1 = 'a'
s2 = 'ab'
s3 = 'abc'
s4 = 'abcd'
print(f'{s1:>10}')
print(f'{s2:>10}')
print(f'{s3:>10}')
print(f'{s4:>10}')
Output:
a
ab
abc
abcd
You can also use format specifiers within the curly braces to control the formatting of your expressions. Here are some common format specifiers you might use with f-strings:
Specifier | Description | Example | Output |
---|---|---|---|
< | Left-align the string within the available space. | f"{'Alice':<10}" | Alice |
> | Right-align the string within the available space. | f"{'Alice':>10}" | Alice |
^ | Center-align the string within the available space. | f"{'Alice':^10}" | Alice |
d | Decimal integer. | f"{123:d}" | 123 |
b | Binary format. | f"{123:b}" | 1111011 |
o | Octal format. | f"{123:o}" | 173 |
x | Hexadecimal format (lowercase). | f"{123:x}" | 7b |
X | Hexadecimal format (uppercase). | f"{123:X}" | 7B |
, | Use comma as a thousand separator. | f"{1234567:,}" | 1,234,567 |
_ | Use underscore as a thousand separator. | f"{1234567:_}" | 1_234_567 |
e | Exponential notation (lowercase). | f"{1234567.89:e}" | 1.234568e+06 |
E | Exponential notation (uppercase). | f"{1234567.89:E}" | 1.234568E+06 |
f | Fixed-point notation. | f"{1234.56789:f}" | 1234.567890 |
g | General format. | f"{1234.56789:g}" | 1234.57 |
% | Percentage. | f"{0.1234:%}" | 12.340000% |
.nf | Fixed-point notation with n digits after the decimal point. | f"{1234.56789:.2f}" | 1234.57 |
.ne | Exponential notation with n digits after the decimal point. | f"{1234.56789:.2e}" | 1.23e+03 |
.ng | General format with n significant digits. | f"{1234.56789:.2g}" | 1.2e+03 |
w | Minimum width of the field. | f"{123.456:10}" | 123.456 |
.p | Precision for floating-point numbers or maximum length for strings. | f"{123.456789:.2f}" | 123.46 |
%Y-%m-%d %H:%M:%S | Date and time formatting using datetime module. | f"{datetime.now():%Y-%m-%d %H:%M:%S}" | 2024-06-10 14:23:45 |
Here are some additional examples combining different specifiers:
Example | Description | Output |
---|---|---|
f"{'Bob':<10} Age: {25:<5}" | Left-align string and integer. | Bob Age: 25 |
f"Balance: ${1234.56789:,.2f}" | Comma separator and 2 decimal places. | Balance: $1,234.57 |
f"Hex: {25:x}, Binary: {25:b}, Octal: {25:o}" | Different number formats. | Hex: 19, Binary: 11001, Octal: 31 |
f"Current time: {datetime.now():%Y-%m-%d %H:%M:%S}" | Date and time formatting. | Current time: 2024-06-10 14:23:45 |
Do comment if you have any doubts or suggestions on this Python string format topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.