Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Formatting using format() method
📖 Scenario: You are preparing a simple report card for students. You want to display their names and scores neatly aligned in columns.
🎯 Goal: Build a program that formats and aligns student names and scores using the format() method.
📋 What You'll Learn
Create a dictionary with student names and their scores
Create a variable for the width of the name column
Use a for loop to format each student's name and score using the format() method
Print the formatted output
💡 Why This Matters
🌍 Real World
Formatting text output is useful for creating readable reports, invoices, or tables in console applications.
💼 Career
Many jobs require displaying data clearly. Knowing how to format strings helps in data presentation and user interfaces.
Progress0 / 4 steps
1
Create the student scores dictionary
Create a dictionary called students with these exact entries: 'Alice': 85, 'Bob': 92, 'Charlie': 78
Python
Hint
Use curly braces {} to create the dictionary and separate entries with commas.
2
Set the width for the name column
Create a variable called name_width and set it to 10 to define the width for the name column
Python
Hint
Just assign the number 10 to the variable name_width.
3
Format each student's name and score
Use a for loop with variables name and score to iterate over students.items(). Inside the loop, create a variable line that formats name left-aligned in a field of width name_width and score right-aligned in a field of width 3 using the format() method
Python
Hint
Use {:<{width}} to left-align the name and {:>3} to right-align the score.
4
Print the formatted lines
Inside the for loop, add a print(line) statement to display each formatted student name and score
Python
Hint
Use print(line) inside the loop to show each formatted line.
Practice
(1/5)
1.
What does the format() method do in Python?
easy
A. It inserts values into a string at placeholders marked by {}.
B. It changes the case of a string to uppercase.
C. It splits a string into a list of words.
D. It removes whitespace from the start and end of a string.
Solution
Step 1: Understand the purpose of format()
The format() method is used to insert values into a string where {} placeholders are present.
Step 2: Compare options
Only It inserts values into a string at placeholders marked by {}. correctly describes this behavior. Other options describe different string methods.
Final Answer:
It inserts values into a string at placeholders marked by {} -> Option A
Quick Check:
format() inserts values into {} [OK]
Hint: Remember: {} marks where values go in format() [OK]
Common Mistakes:
Confusing format() with upper() or strip()
Thinking format() splits strings
Assuming format() changes string case
2.
Which of the following is the correct syntax to insert the value 42 into the string using format()?
"The answer is {}"._____
easy
A. "format[42]"
B. "format 42"
C. "format{42}"
D. "format(42)"
Solution
Step 1: Recall correct method call syntax
The format() method is called with parentheses and the value inside, like format(42).
Step 2: Check options for correct syntax
Only "format(42)" uses parentheses correctly. Options A, C, and D use incorrect brackets or missing parentheses.
Final Answer:
"format(42)" -> Option D
Quick Check:
Method calls use parentheses () [OK]
Hint: Use parentheses () to call methods like format() [OK]
Common Mistakes:
Using square brackets [] instead of parentheses
Using curly braces {} instead of parentheses
Omitting parentheses when calling methods
3.
What is the output of the following code?
print("{0} + {1} = {2}".format(3, 4, 3+4))
medium
A. "7 + 7 = 14"
B. "3 + 4 = 7"
C. "3 + 4 = 3+4"
D. "{0} + {1} = {2}"
Solution
Step 1: Understand placeholders and arguments
The placeholders {0}, {1}, {2} are replaced by the first, second, and third arguments of format(), which are 3, 4, and 7 respectively.
Step 2: Substitute values into the string
Replacing placeholders gives the string "3 + 4 = 7".
Final Answer:
"3 + 4 = 7" -> Option B
Quick Check:
Placeholders replaced by arguments [OK]
Hint: Numbered placeholders match format() arguments by position [OK]
Common Mistakes:
Printing placeholders literally without replacement
Confusing expression with string output
Mixing up argument order
4.
Find the error in this code snippet:
"Hello, {}".format
medium
A. format() cannot be used with strings.
B. Incorrect placeholder syntax; should be {0}.
C. Missing parentheses to call the format method.
D. The string should use single quotes instead of double quotes.
Solution
Step 1: Check method call syntax
The code uses format without parentheses, so the method is not called.
Step 2: Identify the fix
Adding parentheses like format() calls the method and inserts values.
Final Answer:
Missing parentheses to call the format method. -> Option C
Quick Check:
Methods need () to execute [OK]
Hint: Always add () to call methods like format() [OK]
Common Mistakes:
Forgetting parentheses when calling methods
Thinking placeholders must be numbered
Believing quotes style affects format()
5.
How would you format the number 7.12345 to show only two decimal places using format()?
"{:.2f}".format(7.12345)
What is the output?
hard
A. "7.12"
B. "7.12345"
C. "7.1"
D. "7.00"
Solution
Step 1: Understand format specifier :.2f
This means format the number as a float with 2 digits after the decimal point.
Step 2: Apply formatting to 7.12345
The number rounds to 7.12 when limited to two decimal places.
Final Answer:
"7.12" -> Option A
Quick Check:
Two decimals with :.2f rounds number [OK]
Hint: Use {:.2f} to format floats with 2 decimals [OK]