Skip to content

Instantly share code, notes, and snippets.

@nayan108
nayan108 / Python String Formats.md
Last active January 10, 2024 07:47
Python String Formatting

Python String Formats

Number formatting

The following table shows a variety of ways to format numbers with Python's rookie str.format(), including examples of floating-point number formatting and integer formatting. You can use print("FORMAT".format(NUMBER)); to run the example, so you can run: print("{:.2f}".format(3.1415926)); to get the output of the first example.

Number Format Output Description
3.1415926 {:.2f} 3.14 Two decimal places
3.1415926 {:+.2f} +3.14 Two decimal places with signs