F-strings

Programs frequently need to include the values of variables inside strings. Instead of concatenating strings with the plus symbol, Python's f-strings let you use curly braces inside strings to include variables.

Learning Objectives

You should be able to:

  • Describe the benefits of using f-strings over string concatenation
  • Use f-strings

Video Walkthrough

Use this video to follow along with the steps in this lab.

Using F-strings

Python's f-strings were first included in Python version 3.6. Most new code uses f-strings to some extent.

The following code shows how verbose (and error-prone) it can be to create strings solely through concatenation.

name = "Alice"
friend = "Bob"
day = "Tuesday"
minutes = 60
message = "On " + day + ", " + name + " and " + friend + " will hang out for " + str(minutes) + " minutes."
print(message)

It is easy to get lost in the mix of quotation marks and plus symbols. Care has to be taken to convert the minutes variable to a string.

Python's f-strings make this easier. The following code demonstrates how they are used.

name = "Alice"
friend = "Bob"
day = "Tuesday"
minutes = 60
message = f"On {day}, {name} and {friend} will hang out for {minutes} minutes."
print(message)

Line 5 includes the f-string. The letter "f" must be placed before the quotation mark to tell Python to interpret the variables in the string. The variable names are put in curly braces. Notice that Python figures out that minutes is an integer and automatically converts it to a string when needed.

F-string exercise

  • Launch python from the terminal to start an interactive Python shell.
  • Run the following code.
ip = "10.1.2.15"
f"The IP address is {ip}"

You should see the output:

'The IP address is 10.1.2.15'

You can use integers with f-strings without having to convert them to strings manually.

age = 5
f"My sister is {age} years old."
  • You should see the output:
'My sister is 5 years old.'
  • Decimal numbers can be trickier. Run the following code:
fraction = 2/3
f"{fraction} of the pie has been eaten."

The output should be something like:

.6666666666666667 of the pie has been eaten.
  • That's a few more decimal places than needed.
  • F-string allows you to format decimal numbers. In this case, we might only want 2 decimal places after the zero. In this case, we can format the variable with ":.2" by putting that in the curly brace after the variable name.
f"{fraction:.2} of the pie has been eaten."
  • The above code produces a much more readable output:
'0.67 of the pie has been eaten.'

Mad Lib Challenge

  • Create a file called madlib.py.
  • Use the input() function to prompt the user to input 2 adjectives, 2 nouns, 2 verbs, and an adverb.
    • (Use a separate line for each input() function call.)
  • Use f-strings to combine the variables into a brief string.
  • Print the string to the terminal.

Reflection

  • Why is combining strings such an important part of coding?

Key Terms

  • Python f-strings: A feature introduced in Python 3.6 that provides a concise and readable way to embed expressions inside string literals using curly braces {}. F-strings, or formatted string literals, are prefixed with the letter f. They allow for inline expression evaluation and formatting, making string interpolation more convenient and efficient.