Python: 14 String Formatting

There are a lot of different ways to format and display text in Python.

You can find a lot of excellent reference materials on W3 Schools.

Using { } inside a string will allow the user to insert the contents form a variable using the format() function.

The format function takes any number of arguments inside the ().

The first { } will take the first argument the second { } the second argument and so on.

You can also format the display :.2f means format to 2 decimal paces.

format_this = 2.7456
print("Default formatting {}".format(format_this))
print("Two decimal places {:.2f}".format(format_this))

You might also like