Get startedGet started for free

Formatted String Literals ("f" strings)

We've been using plain strings with "" or '' in this class so far, but there are several types of strings and blend variables with them. the most recent addition of a string type to Python is the "f-strings", which is short for formatted string literals. "F-strings" make it easy to mix strings with variables and formatting to help get exactly the output you want and you make them by prefacing the quotes with the letter f like f"". If you want to include a variable within a string you can use the {} around the variable in an f-string to insert the variable's value into the string itself. For example if we had a variable count with the number 12 stored it in, we could make an f-string like f"{count} cookies", which would output the string "12 cookies" when printed. The list top_ten_girl_names contains tuples that correspond to the top_ten_rank and name for each position.

This exercise is part of the course

Data Types in Python

View Course

Exercise instructions

  • Loop over the top_ten_girl_names list and use tuple unpacking to get the top_ten_rank and name.
  • Print out each rank and name like this Rank #: 1 - Jada where the number 1 is the rank and Jada is the name.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Loop over top_ten_girl_names and unpack each tuple into top_ten_rank and name
for ____, ____ in ____:
  	# Print each name in the proper format
    print(____"Rank #: ____ top_ten_rank ____ - { ____ }")
Edit and Run Code