Skip to main content
Enchanted Code

Python 3 string formatting tutorial

tutorial, python

Intro

This tutorial will teach you about the formatting features in the string data-type in Python.

Requirements

  • Know Python 3 strings if not read here
  • Know Python 3 functions if not read here

What are they?

Formatting strings allow you to create a string while including variables with different data-types while maintaining good code readability.

printf-style

The first one is using the % operator. This one is no longer recommended to use by the Python docs, however it’s still being included as it still has it’s uses for the standard libraries logging module.

It allows you to easily add integers and floats to a certain number of decimal places. Which can be useful depending on what your code is trying to achieve. However, I think they are not that easy to read and can make the code more messy.

name = "Leo"
score = 200
formatted_str = "Hello %s, your score is %d" % (name, score)
print(formatted_str)

When run you will see the following output: Hello Leo, your score is 200.

There are some different arguments you can use, these are listed in the table below:

Operator Description
%s String
%d Integer
%f Float
%.<num of digits>f Float to a fixed decimal point

.format()

Ok now let’s use the “.format()” method. It works by adding “curly braces” in the string with a index and then running the “format” method on the chosen string; which has the same number of parameters as the indexes in the string.

As you can see in the below code uses the .format() function on the string. This reduces a lot of code and makes it easier to read especially when there are quite a few variables used, compared to when uses concatenation.

name = "Leo"
formatted_str = "Hello {0}!".format(name)
print(formatted_str)

When we run the code it should output: Hello Leo!.

Formatted string literals

Now we will move on to the most modern (as of Python 3.9), Formatted string literals or f-strings.

f-strings were added to Python in version 3.6

As you will see when declaring a string you have to put a f in front of the speech marks.

As you can see in the below code it uses {} with the variable name inside, I find this is a lot cleaner and easy to read than all of the other methods however, that is just personal opinion. They are also evaluated at runtime so you can do things like {2 * 4}, they can also contain functions and pretty much everything else.

If you want to use “curly braces” as a character you will need to use: {{ }}.

name = "Leo"
formatted_str = f"Hello {name}"
print(formatted_str)

When we run the code it should output: Hello Leo.

Conclusion

As you can see that there are lots of ways we can modify strings in Python. f-string being the most modern approach, however as a programmer it is your chose on what method you choose.

There is many more features of these methods that are not covered by this tutorial, these can of course be found at the official Python documentation.