Python String Interpolation

String Interpolation is the process of substituting values of variables into placeholders in a string, sounds like string concatenation right! But without using + or concatenation methods.

Let’s see how many ways string interpolation works in Python.

%-formatting

It’s a feature provided by Python which can be accessed with a % operator. This is similar to printf style function in C.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate
# string interpolation
  
  
n1 = 'Hello'
n2 ='GeeksforGeeks'
  
# for single substitution
print("Welcome to % s"% n2)
  
# for single and multiple substitutions () mandatory
print("% s ! This is % s."%(n1, n2))

chevron_right


Output



Welcome to GeeksforGeeks
Hello! This is GeeksforGeeks.

Let’s say its just a complicated version, but we can use it if we have a lot of variables to get substituted in the string as we don’t always want to use (“string”+variable+“string”+variable+variable+“string”) this representation. So for this purpose, we can go with %-formatting.

Note: To know more about %-formatting click here.

Str.format()

str.format() is one of the string formatting methods in Python3, which allows multiple substitutions and value formatting. This method lets us concatenate elements within a string through positional formatting.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate
# string interpolation
  
  
n1 = 'Hello'
n2 ='GeeksforGeeks'
  
# for single substitution
print('Hello, {}'.format(n1))
  
# for single or multiple substitutions
# let's say b1 and b2 are formal parameters 
# and n1 and n2 are actual parameters
print("{b1}! This is {b2}.".format(b1 = n1, b2 = n2))
  
# else both can be same too
print("{n1}! This is {n2}.".format(n2 = n2, n1 = n1))

chevron_right


Output

Hello, Hello
Hello! This is GeeksforGeeks.
Hello! This is GeeksforGeeks.

Note: To know more about str.format() click here.

f-strings

PEP 498 introduced a new string formatting mechanism known as Literal String Interpolation or more commonly as F-strings (because of the leading f character preceding the string literal). The idea behind f-strings is to make string interpolation simpler.

To create an f-string, prefix the string with the letter “ f ”. The string itself can be formatted in much the same way that you would with str.format(). F-strings provide a concise and convenient way to embed python expressions inside string literals for formatting.



Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate
# string interpolation
  
  
n1 = 'Hello'
n2 ='GeeksforGeeks'
  
# f tells Python to restore the value of two
# string variable name and program inside braces {}
print(f"{n1}! This is {n2}")
  
# inline arithmetic
print(f"(2 * 3)-10 = {(2 * 3)-10}")

chevron_right


Output

Hello! This is GeeksforGeeks
(2*3)-10 = -4

Note: To know more about f-strings click here.

String Template Class

In String module, Template Class allows us to create simplified syntax for output specification. The format uses placeholder names formed by $ with valid Python identifiers (alphanumeric characters and underscores). Surrounding the placeholder with braces allows it to be followed by more alphanumeric letters with no intervening spaces. Writing $$ creates a single escaped $:

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate
# string interpolation
  
  
from string import Template
  
n1 = 'Hello'
n2 ='GeeksforGeeks'
  
# made a template which we used to 
# pass two variable so n3 and n4 
# formal and n1 and n2 actual
n = Template('$n3 ! This is $n4.')
  
# and pass the parameters into the template string.
print(n.substitute(n3 = n1, n4 = n2))

chevron_right


Output

Hello Hello! This is GeeksforGeeks.

Note: To know more about String Template class click here.




My Personal Notes arrow_drop_up

BERJAYA
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.