Home > Backend Development > Python Tutorial > Python program example demonstrating string interpolation

Python program example demonstrating string interpolation

王林
Release: 2023-09-19 10:53:19
forward
1123 people have browsed it

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.

Method 1: Using f-string

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.

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)
Copy after login

Output

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

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)
Copy after login

Output

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

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 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)
Copy after login

Output

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

in conclusion

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template