0
0
Pythonprogramming~5 mins

Formatting using format() method in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A'Good Morning'
B'{} {}'
C'Morning Good'
D'Good{}Morning'
How do you format the number 7.12345 to show only 3 decimal places?
A'{:.3}'.format(7.12345)
B'{3f}'.format(7.12345)
C'{:.3f}'.format(7.12345)
D'{3}'.format(7.12345)
What does '{1} {0}'.format('first', 'second') output?
A'first second'
B'second first'
C'{1} {0}'
D'first first'
Which is the correct way to use named placeholders?
A'Hello {0}'.format(name='Bob')
B'Hello {}'.format(name='Bob')
C'Hello {name}'.format('Bob')
D'Hello {name}'.format(name='Bob')
What will 'Price: ${:.2f}'.format(4.5) output?
A'Price: $4.50'
B'Price: $4.5'
C'Price: ${:.2f}'
D'Price: $4'
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.