Kodeclik Blog


Formatting strings in Python

Formatting strings refers to pretty printing strings and Python provides multiple ways to format strings. Here are four ways to whet your appetite!

Method 1: The % operator

The most straightforward way to format strings is to use the % operator. It is easiest to illustrate how this works using an example. Consider the following code:
It outputs:
You can think of “%s” as a placeholder, in this case for a string. This string therefore has two placeholders and to substitute values for these placeholders you must use the % operator and pass on two arguments, one for each of these placeholders. Here is an example:
This outputs:
Let us consider another example but this time instead of printing strings, we will print numbers using %d (for integers) and %f (for floating point numbers, or decimals).
This outputs:
You can control the resolution of the floating point numbers like so:
This will output:
Here is a Python program to print multiplication tables.
This will output:
Note how the numbers are not quite aligned because some of them are two digits and some are single digits. We can pretty print them, like so:
This will output:

Method 2: The format() method

The format() method is the more modern way to format strings. It works as follows:
Note that the curly braces denote placeholders and the arguments to the format() method denote the values that are substituted for these placeholders. Running this program yields:
Note that the number of arguments to format() must match the number of placeholders in the string. If we do:
We will get an error:
(Additional arguments will not cause an error.)
To make your code more readable, you can provide names for the placeholders.
This will output:
Note that with this approach the order in which you pass arguments to format does not matter. The template has animal1 before animal2 but in the format argument we specify animal2 before animal1.
You can specify the same formatting instructions as before:
This will give rise to the output:

Method 3: F-strings

F-strings are considered by many programmers to be the more natural way to do string formatting. Here’s how they work with the same example as above.
This will output:
Notice how the F-string expression literally substitutes the current known values of the variables in each placeholder location.
F-strings are also considered to be the faster way to format strings.

Method 4: Python string Template class

In this final approach, the notions of template and substitutes is made explicit using the Template class from the Python String module.
This outputs:
There you have it! You have learnt different ways to format strings - which one is your favorite?
Now that you know how to format strings, you can use the ideas you learnt here to send pretty formatted emails. Take a look at our Python send email post for more!
Interested in more things Python? See our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.
  • ABOUT

Copyright @ Kodeclik 2023. All rights reserved.