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:
# 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)) |
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:
# 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)) |
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:
# 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}") |
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:
# 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)) |
Output
Hello Hello! This is GeeksforGeeks.
Note: To know more about String Template class click here.
Recommended Posts:
- Gauss's Forward Interpolation
- Interpolation Search visualizer using PyQt5
- Python | Check if given string can be formed by concatenating string elements of list
- String slicing in Python to check if a string can become empty by recursive deletion
- Python - Remove front K characters from each string in String List
- Python | Merge Tuple String List values to String
- Python | Sorting string using order defined by another string
- Python | Check if string ends with any string in given list
- Python - Length of shortest string in string list
- Python | Delimited String List to String Matrix
- Python | Sort each String in String list
- Python | Check if a given string is binary string or not
- Python - Remove String from String List
- String slicing in Python to rotate a string
- String Alignment in Python f-string
- String to Int and Int to String in Python
- Python | Add one string to another
- Python String | max()
- Python String | min()
- Python String
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.


