Home >Backend Development >Python Tutorial >Python program example demonstrating string interpolation
In Python, we can use f-string, % operator and format() method to demonstrate string interpolation. String interpolation is the process of inserting dynamic data or variables into a string. It is useful when using variables or expressions to form strings without using any string formatting or string concatenation. In this article, we will see how to do string interpolation using Python.
An f-string is a string literal starting with f or F. The prefix f or F indicates that the string is an f-string. The string contains expressions enclosed in curly braces {}. These expressions can have dynamic values that are evaluated at runtime.
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)
My name is John. I am 25 years old and 1.75 meters tall.
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.
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)
My name is John. I am 25 years old and 1.75 meters tall.
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.
In the following example, we initialize three variables, namely name, age, and height, and then use the % operator to create a message string. The string contains expressions specified as placeholders, represented by %s, %d, and %f. The values of these placeholders are passed to the % operator via tuples.
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)
My name is John. I am 25 years old and 1.75 meters tall.
String interpolation allows you to create a string containing variables and expressions. The values of these expressions or variables are dynamic and obtained at runtime. Python provides methods like f-string, format method, and % operator to create string interpolation. In this article, we learned about all three methods with examples.
The above is the detailed content of Python program example demonstrating string interpolation. For more information, please follow other related articles on the PHP Chinese website!