Format Directives

The .format() function

Similar to what we just saw with f-strings, we can also easily format expressions in Python using the built-in format() function. This allows us to insert variables or expressions inside curly braces to more easily format them. For example:

					text1 = "My name is {} and I am {} years old".format("John", 36)

# will print: My name is John and I am 36 years old

This is the simplest way to embed the expressions within the format function into the string that calls it: by using empty {} braces for each of the passed parameters.

It may help us stay more organized by using either of the following options as well:

					text1 = "My name is {} and I am {} years old".format("John", 36)
text2 = "My name is {0} and I am {1} years old".format("John", 36)
text3 = "My name is {first} and I am {age} years old".format(first = "John", age = 36)

# all three string will be "My name is John and I am 36 years old"

Each of the above strings will all result in the same value. It is preferable though to use the syntax provided in text2 and text3, as this allows more flexibility in how we insert the expressions into the curly braces, and also makes our code a bit more readable. Text2 allows us to index the passed parameters starting at 0 (similar to a list), while text3 allows us to index them according a key-value pair (similar to a dictionary). Here’s another example:

Using string formatting might also help us output similarly formatted print statements in a more efficient way. Notice how the code below allows to save a bit of copying and pasting since we use the text variable to replicate similar patterns in the strings we want to print out:

					text = "{0} wears number {1} for the {2}"
print(text.format("Patrick Mahomes", 15, "Kansas City Chiefs"))
print(text.format("Justin Fields", 1, "Chicago Bears"))
print(text.format("Aaron Donald", 99, "Los Angeles Rams"))

Format Specifiers

Beyond just more efficiently formatting expressions within strings, the format() function also allows us to define how individual values are presented. One of the most common ones is the float specifier, which allows us to round decimal values according to a number of placeholders:

					pi = 3.14159265
print("Pi rounded to 3 decimal places is {:.3f}".format(pi)))
# Prints "Pi rounded to 3 decimal places is 3.142"
print("Pi rounded to 1 decimal place is {:.1f}".format(pi)))
# Prints "Pi rounded to 1 decimal places is 3.1"
print("Pi rounded to the nearest integer is {:.0f}".format(pi))
# Prints "Pi rounded to the nearest integer is 3"

Notice the syntax we need to round decimals: inside the curly braces we type “:.” followed by the number of places we want to round to, and finish the expression with an “f”.

There are also enhanced ways we can format numbers using formatting directives such as including commas for numbers larger than one thousand, adding trailing zeros to decimals, and aligning text to the left, center, or right of the console when it gets printed. You can see a list of these here.

Replit Practice