Skip to content

Python String Interpolation

How to use Python's string interpolation methods like f-strings, %-formatting, str.format(), and Template strings, for clean and efficient variable substitution in strings.

String interpolation is a process of substituting values of variables into placeholders in a string.

This chapter will discuss several ways to perform string interpolation.

String Interpolation

f-strings

Starting with Python 3.6 a new literal prefix f was added in order to implement a new way of string interpolation.

This new way of formatting strings is powerful and easy to use ant it allows access to Python expressions embedded in string constants.

name = 'Tom'
age = 23
print(f'My name is {name} and I am {age} years old!')

Output

My name is Tom and I am 23 years old!

You can embed arbitrary Python expressions in f-strings. For example:

num = 100
print(f'{num} minus 21 is {num - 21}')

%-formatting

With the % operator is possible to use a built-in operation to do simple string interpolation.

For example

print("%s %s" %('Hello','World!')) # Hello World!

Note that in the previous example, to make multiple substitutions in a single string, and because the % operator accepts only one argument, it was necessary to wrap the right side in a tuple.

n1 = 'Tom'
n2 = 'TutorialReference'
 
print("Welcome to % s" % n2)
 
print("I am %s and this is this is %s." % (n1, n2))

Output

Welcome to TutorialReference
I am Tom and this is This is TutorialReference.

Str.format()

The str.format() method works by inserting into a string one or more substitute fields and placeholders defined by a pair of curly brackets {}. The value is inserted into the placeholders and concatenated with the string passed as a parameter in the format function.

name = 'Tom'
print('Hello, I am {}'.format(name))

Output

Hello, I am Tom

You can also use the variable name inside the curly brackets {} to use format function parameters in any order.

name = 'Tom'
age = 23
print('Hello, I am {name} and I am {age} years old'.format(name=name, age=age))
print('Hello, I am {age} and I am {name} years old'.format(name=name, age=age))

Output

Hello, I am Tom and I am 23 years old
Hello, I am 23 and I am Tom years old

Template Strings

Template Strings is simpler and less powerful mechanism of string interpolation.

For example:

from string import Template
name = 'Tom'
age = 23
new = Template('Hello, I am $name and I am $age years old.')
print(new.substitute(name = name, age = age))

Output:

Hello, I am Tom and I am 23 years old.

Summary

  1. The %-format method is a very old method for interpolation and it is not advisable to use it, as it reduces the readability of the code.
  2. In the str.format() method we pass the string object to the format() function for string interpolation.
  3. In the template method, a template is created by importing the template class from the constructed string module.
  4. The Literal String Interpolation method is a powerful, easy-to-use interpolation method that increases code readability.
Navigation