Interpolation in Python 3.10

Find this useful? Support us: Star on GitHub 6
Category: String | Language: Python 3.10

In Python 3.10, there are several new ways to interpolate strings. The new string interpolation methods are simpler and more readable than previous methods. Here are some examples of the different ways to interpolate strings in Python 3.10.

1. F-Strings

F-Strings, or Formatted String Literals, are a new way of interpolating strings in Python 3.10. To use an F-String, prefix the string with the letter "f" and then wrap any expressions to be interpolated in curly braces "{}". Here's an example:

name = "Alice"
age = 30
print(f"My name is {name} and I'm {age} years old.")

Output:

My name is Alice and I'm 30 years old.

2. Template Strings

Template Strings are another new way of interpolating strings in Python 3.10. To use a Template String, import the string module and create a Template object. Then use the "$" symbol to mark variable names in the string. Here's an example:

from string import Template

name = "Bob"
age = 25

template = Template("My name is $name and I'm $age years old.")
print(template.substitute(name=name, age=age))

Output:

My name is Bob and I'm 25 years old.

3. String Concatenation

String Concatenation is the oldest and most basic form of string interpolation in Python. You can concatenate string literals and variables using the "+" operator. Here's an example:

name = "Charlie"
age = 20

print("My name is " + name + " and I'm " + str(age) + " years old.")

Output:

My name is Charlie and I'm 20 years old.