Python Program to demonstrate the string interpolation


In Python, we can demonstrate string interpolation using the f-string,%operator, and format() method. String Interpolation is the process of inserting dynamic data or variables in the string. It is useful when a string is formed using variables or expressions without using any string formatting or string concatenation. In this article, we will see how we can use Python to do string interpolation.

Method 1 : Using f-string

An f-string is a string literal which starts with f or F. The prefix f or F indicates that the string is an f-string. The string contains the expression which is enclosed in curly braces {}. These expressions can have dynamic values which are evaluated at runtime.

Example

In the below example, we create three variables namely name, age, and height whose values are initialized. A message is created using an f-string in which name, age, and height are expressions that are enclosed in curly braces. The value of these expressions is taken from the variables(name, age, and height) at runtime.

name = 'John'
age = 25
height = 1.75

message = f"My name is {name}. I am {age} years old and {height} meters tall."
print(message)

Output

My name is John. I am 25 years old and 1.75 meters tall.

Method 2 : Using the format() method

The format() method is used to do string interpolation by inserting values in a string using placeholders. These placeholders are represented in the string using curly braces {}. The values at these placeholders are taken from the .format() attribute at the end of the string.

Example

In the below example, we first initialize three variables namely name, age, and height. We then create a message using a string with placeholders in it represented by curly braces{}.The format() method specifies the values at these placeholders.

name = 'John'
age = 25
height = 1.75

message = "My name is {}. I am {} years old and {} meters tall.".format(name, age, height)
print(message)

Output

My name is John. I am 25 years old and 1.75 meters tall.

Method 3 : Using the % operator

The % operator works similarly to the use of % operators in printf() function in C-programming. The string contains expressions in the form of %s,%d,%f, etc which specify the type of value for example %s specifies string,%d specifies integer,%f specifies float value, etc.

Example

In the below example, we initialize three variables namely name, age, and height, and then create a message string using the % operator. The string contains expressions in the form of placeholders which are specified using %s,%d, and %f. The values for these placeholders are passed using tuples to the % operator.

name = 'John'
age = 25
height = 1.75

message = "My name is %s. I am %d years old and %.2f meters tall." % (name, age, height)
print(message)

Output

My name is John. I am 25 years old and 1.75 meters tall.

Conclusion

String interpolation allows you to create a string that contains variables and expressions. The value of these expressions or variables is dynamic and is taken at runtime. Python provides methods like f-string, format method, and %operator to create string interpolation. In this article, we understood all three methods with examples.

Updated on: 17-Apr-2023

106 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements