- Learn Python by Building Data Science Applications
- Philipp Kats David Katz
- 76字
- 2021-06-24 13:05:58
Formatting mini-language
In all cases, you can format injected values along the way by using Python's formatting mini-language. Just add a colon after your expression in the curly brackets, and then write the mini-language notation. Take this example:
>>> pct = .345
>>> value = 45500
>>> f‘Price grew by {pct:.1%} or ${value:,}'
'Price grew by 34.5% or 45,500'
More information on the Python formatting language can be accessed via the official documentation (https://docs.python.org/3/library/string.html#formatspec).