Recall & Review
beginner
What does the
format() method do in Python?It inserts values into a string at placeholders marked by curly braces
{}, allowing you to create formatted strings easily.Click to reveal answer
beginner
How do you insert multiple values using
format()?Use multiple curly braces
{} in the string and pass the values in order to format(). For example: '{} {}'.format('Hello', 'World') results in 'Hello World'.Click to reveal answer
intermediate
How can you specify the order of values in
format()?Use numbers inside the curly braces to indicate the position of the argument. For example:
'{1} {0}'.format('first', 'second') results in 'second first'.Click to reveal answer
intermediate
How do you format a number to show 2 decimal places using
format()?Use a format specifier inside the braces like
{:.2f}. For example: '{:.2f}'.format(3.14159) results in '3.14'.Click to reveal answer
intermediate
What happens if you use named placeholders in
format()?You can name placeholders like
{name} and pass values as keyword arguments. For example: 'Hello {name}'.format(name='Alice') results in 'Hello Alice'.Click to reveal answer
What will
'{} {}'.format('Good', 'Morning') output?✗ Incorrect
The
format() method replaces the curly braces with the values in order, so it outputs 'Good Morning'.How do you format the number 7.12345 to show only 3 decimal places?
✗ Incorrect
The format specifier
{:.3f} means 3 decimal places floating point.What does
'{1} {0}'.format('first', 'second') output?✗ Incorrect
The numbers inside braces specify the argument index, so {1} is 'second' and {0} is 'first'.
Which is the correct way to use named placeholders?
✗ Incorrect
Named placeholders require matching keyword arguments in format().
What will
'Price: ${:.2f}'.format(4.5) output?✗ Incorrect
The number is formatted to 2 decimal places, so 4.5 becomes 4.50.
Explain how to use the
format() method to insert multiple values into a string.Think about how you write placeholders and how you pass values to fill them.
You got /3 concepts.
Describe how to format a floating-point number to show exactly two decimal places using
format().Look for the part inside the curly braces that controls decimal places.
You got /3 concepts.