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.
Deze oefening maakt deel uit van de cursus
Data Types in Python
Oefeninstructies
- Loop over the
top_ten_girl_nameslist and use tuple unpacking to get thetop_ten_rankandname. - Print out each rank and name like this
Rank #: 1 - Jadawhere the number 1 is the rank and Jada is the name.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
# 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 ____ - { ____ }")