Articles
Screencasts
Exercises
Training
Pastebin
Gift
/
Sign Up
Sign In
String formatting modifiers for numbers.
Editable Code
""" String formatting modifiers for numbers. https://www.pythonmorsels.com/string-formatting/ """ num = 4 print("{num:.2f}", f"{num:.2f}", "2 digits after decimal", sep="\t") print("{num:02}", f"{num:02}", "0-pad to 2 digits", sep="\t") print("{num: 3}", f"{num: 3}", "space-pad to 3 digits", sep="\t") print("{num:.0%}", f"{num:.0%}", "format as percent with 0 digits after decimal", sep="\t") print("{num**6:,}", f"{num**6:,}", "add commas as thousands separators", sep="\t")
731 views
Copy
Code copied
Run
in Browser
pym.dev/p/2t36c/
URL copied
Need to share some Python code?
New Python snippet