Skip to content

Python String Interpolation

Syntax

String interpolation in Python is achieved using formatted strings (f-strings) or the str.format() method.

# Using f-strings
name = "Alice"
age = 30
message = f"{name} is {age} years old."
# Using the str.format() method
message = "{} is {} years old.".format(name, age)

Example

# Displaying user details using interpolation
user = "John"
balance = 250.75
print(f"Hello, {user}. Your account balance is ${balance:.2f}.")