String formatting is one of the basic functionalities we explore when learning a new programming language. It is the process of interpolating variables in strings and presenting the strings.

In Python, there are 3 ways of string formatting

  • With % operator
  • With format() string method
  • With string literals, called f-strings

We shall discuss each of the above-mentioned methods and in the end, take a call on which method would be the best choice.

% Operator: I like to call it the “OG string formatting method”

This is the oldest of the string formatting method. We use the modulo % operator which is also known as the “string formatting operator”. Below you can see an example of how we used to do it the OG way.

Fun Fact: In the official Python docs, the %-formatting method is called as the printf-style String Formatting. This is because %-formatting was inherited from the printf function in the C programming language.


We can also inject multiple strings at a time. And to make it more dynamic we can use variables to inject values too.

%s is used to inject strings, similarly %d for integers, %f for floating-point values, %b for binary format. Here is the official documentation if you are interested in taking a deeper dive into all the possible formats, and conversion methods.


That is not it, we can also use multiple format conversion types in a single print statement.

Why don’t you go ahead and give it a shot? Try printing a string by injecting a number first as an integer and then as a float.

Do keep in mind that the %-formatting method is not recommended by the Python docs. Quoting the docs below:

The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer formatted string literals, the [str.format()](<https://docs.python.org/3/library/stdtypes.html#str.format>) interface. Each of these alternatives provides their own trade-offs and benefits of simplicity, flexibility, and/or extensibility.

format() method

The newer way of string formatting in Python is the format() method. It was introduced in Python 2.6 as an improvement to the %-formatting. With the str.format() method, the replacement fields are marked by {} as shown below.


We can also reference variables in any order by referencing their index:


If we insert the variable names, there is the added advantage of being able to pass objects and then reference parameters and methods in between the braces as shown below.


We can also use ** to do this neat trick with dictionaries:

str.format() is definitely an upgrade when compared with %-formatting, but hold on.

f-Strings: A New and Improved Way to Format Strings in Python

The f-strings were introduced to the party in Python 3.6. You can refer PEP 498 to get a deeper view of literal string interpolation.

Formatted string literals or f-strings are string literals that are prefixed with f or F and may contain expressions delimited by curly braces {}. The expressions are evaluated at runtime and then formatted using the __format__ protocol, using the format specifier as an argument. As always, the Python docs are your friend when you want to learn more.

The syntax is similar to the one we used with str.format() but more readable.


As I mentioned earlier it works with a capital letter F too:

Arbitrary Expressions

Because f-strings are evaluated at runtime, we can put any and all valid Python expressions in them. This allows us to do some nifty things.

We could start with something straightforward, like this:

Turn it up a notch and we could call functions like this:

We also have the option of calling a method directly!


And much more. I will be writing an article focusing solely on f-string soon and in that, we will cover f-strings much more extensively.

The final verdict

I did a quick comparison of the three string formatting methods using the timeit module and here are the results

%-format

.format()

f-string

String formatting is one of the basic functionalities we explore when learning a new programming language. It is the process of interpolating variables in strings and presenting the strings.

In Python, there are 3 ways of string formatting

With % operator
With format() string method
With string literals, called f-strings
We shall discuss each of the above-mentioned methods and in the end, take a call on which method would be the best choice.

% Operator: I like to call it the “OG string formatting method”
This is the oldest of the string formatting method. We use the modulo % operator which is also known as the “string formatting operator”. Below you can see an example of how we used to do it the OG way.

Fun Fact: In the official Python docs, the %-formatting method is called as the printf-style String Formatting. This is because %-formatting was inherited from the printf function in the C programming language.

We can also inject multiple strings at a time. And to make it more dynamic we can use variables to inject values too.

%s is used to inject strings, similarly %d for integers, %f for floating-point values, %b for binary format. Here is the official documentation if you are interested in taking a deeper dive into all the possible formats, and conversion methods.

That is not it, we can also use multiple format conversion types in a single print statement.

Why don’t you go ahead and give it a shot? Try printing a string by injecting a number first as an integer and then as a float.

Do keep in mind that the %-formatting method is not recommended by the Python docs. Quoting the docs below:

The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer formatted string literals, the str.format() interface. Each of these alternatives provides their own trade-offs and benefits of simplicity, flexibility, and/or extensibility.

format() method
The newer way of string formatting in Python is the format() method. It was introduced in Python 2.6 as an improvement to the %-formatting. With the str.format() method, the replacement fields are marked by {} as shown below.

We can also reference variables in any order by referencing their index:

If we insert the variable names, there is the added advantage of being able to pass objects and then reference parameters and methods in between the braces as shown below.

We can also use ** to do this neat trick with dictionaries:

str.format() is definitely an upgrade when compared with %-formatting, but hold on.

f-Strings: A New and Improved Way to Format Strings in Python
The f-strings were introduced to the party in Python 3.6. You can refer PEP 498 to get a deeper view of literal string interpolation.

Formatted string literals or f-strings are string literals that are prefixed with f or F and may contain expressions delimited by curly braces {}. The expressions are evaluated at runtime and then formatted using the format protocol, using the format specifier as an argument. As always, the Python docs are your friend when you want to learn more.

The syntax is similar to the one we used with str.format() but more readable.

As I mentioned earlier it works with a capital letter F too:

Arbitrary Expressions
Because f-strings are evaluated at runtime, we can put any and all valid Python expressions in them. This allows us to do some nifty things.

We could start with something straightforward, like this:

Turn it up a notch and we could call functions like this:

We also have the option of calling a method directly!

And much more. I will be writing an article focusing solely on f-string soon and in that, we will cover f-strings much more extensively.

The final verdict
I did a quick comparison of the three string formatting methods using the timeit module and here are the results

%-format

.format()

f-string

As you can see, f-string is faster. Here is a detailed performance comparison that I found on Slackoverflow which comes to the same conclusion.

f-string’s ease of use, flexibility, and readability puts it much ahead of its competitors. But since they were introduced in Python 3.6, go with the format() method if you are working with an older Python version. format() method is not available post-Python 2.6 so we are pretty much stuck with %-formatting if you are working with Python<2.6.


As you can see, f-string is faster. Here is a detailed performance comparison that I found on Slackoverflow which comes to the same conclusion.

f-string‘s ease of use, flexibility, and readability puts it much ahead of its competitors. But since they were introduced in Python 3.6, go with the format() method if you are working with an older Python version. format() method is not available post-Python 2.6 so we are pretty much stuck with %-formatting if you are working with Python<2.6.