String Interpolation in Python
String interpolation techniques and when to use them - explained in 3 minutes.
String interpolation in computer programming is the process of creating string literals from string templates that contain one or more placeholders, by replacing the placeholders with their corresponding values.
Python has three ways of string interpolation. I am going to explain each of these with one or two examples.
f-strings
One of the latest and most developer-friendly approaches is to use the f-strings. F-strings are a concise and efficient way to embed expressions inside string literals.
name = 'John'
age = 25
print(f'My name is {name} and I am {age} years old.')
Let's look at a more complicated example. Having a list comprehension inside of the string interpolation.
vowels = ('a', 'e', 'i', 'o', 'u')
print(f"These are vowels in English: {', '.join(vowels)}")
One thing to note in the above example is, if you have single quotes in the f-string, the outer quotes cannot be single quotes.
str.format()
The format method of the string class is another way to perform string interpolation. It works by putting one or more placeholders in the string and the values to substitute these placeholders with should be passed as arguments to the format method.
name = 'John'
age = 25
print('My name is {} and I am {} years old.'.format(name, age))
One alternative syntax to use the str.format() is to have named placeholders like in the example below and pass keyworded arguments to replace the placeholders. In the below example, I have to replace the name in two places, but I can get away by passing it only once to the string.
name = 'John'
age = 25
print('My name is {name} and I am {age} years old. \
My grandfather was also named {name}'.format(name=name, age=age))
I can also store the string with the placeholders as a template somewhere and perform string substitution on demand.
%-formatting
The last mechanism of formatting a Python string is to use the % operator. This syntax feels similar to the printf style in C. Here, the character after the % tells the compiler what the intended value is to be substituted in its place. Check out the example below.
name = 'John'
age = 25
print('My name is %s and I am %d years old.' % (name, age))
Use the below table as a guide for some of the common % symbols. The full list of symbols is available here.
%-sign | Meaning |
%s | Placeholder for string |
%d | Placeholder for signed integer decimal |
%f | Placeholder for floating point decimal |
%r | Replace with raw data of a variable. |
%x | Replace with hexadecimal value. |
%o | Replace with octal value. |
%c | Replace with special characters. |
Conclusion
There are also Template strings in Python. I did not cover it in this list of interpolation techniques because it is a vast topic of its own.
Out of the three techniques I like to use f-strings the most followed by %-formatting style when I am using logging statements. The str.format() style is preferred when I have to store the string to interpolate separately and inject the values to the placeholders as keyworded arguments. I use these three techniques during coding depending on the scenario.
I would love to hear if I have missed any other techniques or if you guys have any preferences.