Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Interpolating Variables Into a String

Give Feedback

With Python version 3.6, a new string formatting mechanism was introduced. This feature is named the Formatted String Literal or f-string. In this lesson, you’ll learn how to do variable interpolation with f-strings. With this feature, you can specify a variable name directly within an f-string literal, and Python will replace the name with the corresponding value:

>>>
>>> n =20
>>> m = 25
>>> prod = n * m
>>> prod
500
>>> print('The product of', n, 'and', m, 'is', prod)
The product of 20 and 25 is 500

>>> print(f'The product of {n} and {m} is {prod}')
The product of 20 and 25 is 500

>>> print(f'The product of {n} and {m} is {n * m}')
The product of 20 and 25 is 500

>>> f''
''
>>> F''
''

>>> var = 'Bark'
>>> print(f'A dog says {var}!')
A dog says Bark!
>>> print(F"A dog says {var}!")
A dog says Bark!
>>> print(f'''A dog says {var}!''')
A dog says Bark!

>>> print(f'A dog says {var}!")
  File "<input>", line 1
    print(f'A dog says {var}!")
                              ^
SyntaxError: EOL while scanning string literal

Here are some resources on f-strings:

Minh Pham on March 2, 2020

Hi Christopher,

I would like to know how to get the Python Shell you are using in this video to teach. I got Python from MS Appstore, try to configure IDLE but could not get the “hints” poping up every time I type something.

Thanks Minh

Chris Bailey RP Team on March 2, 2020

Hi @Minh Pham, I’m using Bpython, and I put a set of links to resources for it below the video of the second lesson in the series. String Operators It is something you usually can simply pip install, but the resources there will walk you through if you have any issues.

Become a Member to join the conversation.