W3docs

How do I put a variable’s value inside a string (interpolate it into the string)?

In Python, you can use the format() method or f-strings (available in Python 3.6 and above) to interpolate a variable's value into a string.

In Python, you can use the format() method or f-strings (available in Python 3.6 and above) to interpolate a variable's value into a string.

Using the format() method:

Interpolate a variable's value into a string using the format() method

name = "John"
print("My name is {}".format(name))

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

Using f-strings (Python 3.6 and above):

Interpolate a variable's value into a string using the f-strings

name = "John"
print(f"My name is {name}")

Both will output "My name is John".