Python String Interpolation

In this article we will learn about the python string interpolation. Python supports multiple ways to format text strings and these includes %-formatting, sys.format(), string.Template and f-strings.

String interpolation is a process substituting values of variables into placeholders in a string. For instance, if you have a template for saying hello to a person like "Hello {Name of person}, nice to meet you!", you would like to replace the placeholder for name of person with an actual name. This process is called string interpolation.

f-strings

Python 3.6 added new string interpolation method called literal string interpolation and introduced a new literal prefix f. This new way of formatting strings is powerful and easy to use. It provides access to embedded Python expressions inside string constants.

Example 1:

name = 'World'
program = 'Python'
print(f'Hello {name}! This is {program}')

When we run the above program, the output will be

Hello World! This is Python

In above example literal prefix f tells Python to restore the value of two string variable name and program inside braces {}. So, that when we print we get the above output.

This new string interpolation is powerful as we can embed arbitrary Python expressions we can even do inline arithmetic with it.

Example 2:

a = 12
b = 3
print(f'12 multiply 3 is {a * b}.')

When we run the above program, the output will be

12 multiply 3 is 36.

In the above program we did inline arithmetic which is only possible with this method.


%-formatting

Strings in Python have a unique built-in operation that can be accessed with the % operator. Using % we can do simple string interpolation very easily.

Example 3:

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

When we run the above program, the output will be

Hello World

In above example we used two %s string format specifier and two strings Hello and World in parenthesis (). We got Hello World as output. %s string format specifier tell Python where to substitute the value.

String formatting syntax changes slightly, if we want to make multiple substitutions in a single string, and as the % operator only takes one argument, we need to wrap the right-hand side in a tuple as shown in the example below.

Example 4:

name = 'world'
program ='python'
print('Hello %s! This is %s.'%(name,program))

When we run the above program, the output will be

Hello world! This is python.

In above example we used two string variable name and program. We wrapped both variable in parenthesis ()

It’s also possible to refer to variable substitutions by name in our format string, if we pass a mapping to the % operator:

Example 5:

name = 'world' 
program ='python'
print(‘Hello %(name)s! This is %(program)s.’%(name,program))

When we run the above program, the output will be

Hello world! This is python.

This makes our format strings easier to maintain and easier to modify in the future. We don’t have to worry about the order of the values that we’re passing with the order of the values that are referenced in the format string.


Str.format()

In this string formatting we use format() function on a string object and braces {}, the string object in format() function is substituted in place of braces {}. We can use the format() function to do simple positional formatting, just like % formatting.

Example 6:

name = 'world'
print('Hello, {}'.format(name))

When we run the above program, the output will be

Hello,world

In this example we used braces {} and format() function to pass name object .We got the value of name in place of braces {} in output.

We can refer to our variable substitutions by name and use them in any order we want. This is quite a powerful feature as it allows for re-arranging the order of display without changing the arguments passed to the format function.

Example 7:

name = 'world'
program ='python'
print('Hello {name}!This is{program}.'.format(name=name,program=program))

When we run the above program, the output will be

Hello world!This is python.

In this example we specified the variable substitutions place using the name of variable and pass the variable in format().


Template Strings

Template Strings is simpler and less powerful mechanism of string interpolation. We need to import Template class from Python’s built-in string module to use it.

Example 8:

from string import Template
name = 'world'
program ='python'
new = Template('Hello $name! This is $program.')
print(new.substitute(name= name,program=program))

When we run the above program, the output will be

Hello world! This is python.

In this example we import Template class from built-in string module and made a template which we used to pass two variable.


Key Points to Remember:

  1. %-format method is very old method for interpolation and is not recommended to use as it decrease the code readability.
  2. In str.format() method we pass the string object to the format() function for string interpolation.
  3. In template method we make a template by importing template class from built in string module.
  4. Literal String Interpolation method is powerful interpolation method which is easy to use and increase the code readability.