Python String Interpolation
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!
The literal prefix f
tells Python to substitute the value of variables inside the curly brackets {}
. This produces a string with the values substituted for the placeholders
You can embed arbitrary Python expressions in f-strings. For example:
num = 100
print(f'{num} minus 21 is {num - 21}')
# 100 minus 21 is 79
%-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!
%s
string format specifier tell Python where to substitute the value indicated inside parenthesis %( ... )
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'
# for single substitution
print("Welcome to % s" % n2)
# for single and multiple substitutions()
# mandatory
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.
Template
class myst be imported from Python’s built-in string
module to use it.
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
- 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. - In the
str.format()
method we pass the string object to theformat()
function for string interpolation. - In the
template method
, a template is created by importing the template class from the constructed string module. - The
Literal String Interpolation
method is a powerful, easy-to-use interpolation method that increases code readability.