Recall & Review
beginner
What is an f-string in Python?
An f-string is a way to embed expressions inside string literals, using curly braces {} and prefixing the string with the letter 'f'. It makes string formatting easier and more readable.
Click to reveal answer
beginner
How do you include a variable named
name inside an f-string?You write the string with an
f before the quotes and put the variable inside curly braces like this: f"Hello, {name}!".Click to reveal answer
intermediate
How can you format a floating-point number to show only 2 decimal places using f-strings?
Use a format specifier inside the braces like this:
f"{value:.2f}". This rounds the number to 2 decimal places.Click to reveal answer
intermediate
Can you use expressions inside f-strings? Give an example.
Yes! You can put any valid Python expression inside the braces. For example:
f"5 + 3 = {5 + 3}" will output 5 + 3 = 8.Click to reveal answer
advanced
How do you include a literal curly brace character in an f-string?
To include a curly brace, double it: use
{{ for { and }} for }. For example: f"{{Hello}}" outputs {Hello}.Click to reveal answer
What prefix do you add before a string to create an f-string in Python?
✗ Incorrect
The prefix 'f' before the quotes tells Python to treat the string as an f-string.
How would you format the number 3.14159 to show only 2 decimal places using an f-string?
✗ Incorrect
The format specifier '.2f' rounds the number to 2 decimal places.
What will this code print? <br>
name = 'Sam'<br>print(f"Hello, {name}!")✗ Incorrect
The variable 'name' is replaced by its value 'Sam' inside the f-string.
How do you include a literal curly brace '{' in an f-string?
✗ Incorrect
Doubling the brace '{{' or '}}' tells Python to include a literal brace.
Can you put calculations inside the curly braces of an f-string?
✗ Incorrect
F-strings can evaluate any valid Python expression inside the braces.
Explain how to use f-strings to include variables and expressions inside strings.
Think about how you write the string and where you put the variable names.
You got /4 concepts.
Describe how to format numbers with specific decimal places using f-strings.
Look inside the braces for a colon and formatting code.
You got /4 concepts.