Bird
Raised Fist0
Pythonprogramming~10 mins

Formatting using format() method in Python - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to insert the variable name into the string using format().

Python
greeting = "Hello, {}!".[1](name)
print(greeting)
Drag options to blanks, or click blank then click option'
Aappend
Breplace
Cformat
Dinsert
Attempts:
3 left
💡 Hint
Common Mistakes
Using string methods like replace or append instead of format.
Trying to insert the variable without using any method.
2fill in blank
medium

Complete the code to format the number pi to 2 decimal places using format().

Python
pi = 3.14159
formatted_pi = "{:.[1]f}".format(pi)
print(formatted_pi)
Drag options to blanks, or click blank then click option'
A2
B4
C3
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong number for decimal places.
Forgetting the 'f' after the number.
3fill in blank
hard

Fix the error in the code to correctly format the string with two variables name and age.

Python
info = "Name: {}, Age: {}".[1](name, age)
print(info)
Drag options to blanks, or click blank then click option'
Aformat
Bformat_map
Cformat_string
Dformat_args
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like format_string or format_args.
Using format_map which requires a dictionary, not positional arguments.
4fill in blank
hard

Fill both blanks to create a formatted string that aligns name left and score right within 10 spaces.

Python
result = "Name: {0:[1]10} Score: {1:[2]10}".format(name, score)
print(result)
Drag options to blanks, or click blank then click option'
A<
B>
C^
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same alignment for both fields.
Using center alignment ^ instead of left or right.
5fill in blank
hard

Fill all three blanks to format a dictionary data into a string showing key uppercased, value, and only keys with values greater than 10.

Python
formatted = ", ".join("{0}: {1}".format([1], [2]) for k, v in data.items() if v [3] 10)
print(formatted)
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using k.lower() instead of uppercasing keys.
Using wrong comparison operators like < or ==.

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

  1. Step 1: Understand the purpose of format()

    The format() method is used to insert values into a string where {} placeholders are present.
  2. 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.
  3. Final Answer:

    It inserts values into a string at placeholders marked by {} -> Option A
  4. 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

  1. Step 1: Recall correct method call syntax

    The format() method is called with parentheses and the value inside, like format(42).
  2. Step 2: Check options for correct syntax

    Only "format(42)" uses parentheses correctly. Options A, C, and D use incorrect brackets or missing parentheses.
  3. Final Answer:

    "format(42)" -> Option D
  4. 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

  1. 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.
  2. Step 2: Substitute values into the string

    Replacing placeholders gives the string "3 + 4 = 7".
  3. Final Answer:

    "3 + 4 = 7" -> Option B
  4. 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

  1. Step 1: Check method call syntax

    The code uses format without parentheses, so the method is not called.
  2. Step 2: Identify the fix

    Adding parentheses like format() calls the method and inserts values.
  3. Final Answer:

    Missing parentheses to call the format method. -> Option C
  4. 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

  1. Step 1: Understand format specifier :.2f

    This means format the number as a float with 2 digits after the decimal point.
  2. Step 2: Apply formatting to 7.12345

    The number rounds to 7.12 when limited to two decimal places.
  3. Final Answer:

    "7.12" -> Option A
  4. Quick Check:

    Two decimals with :.2f rounds number [OK]
Hint: Use {:.2f} to format floats with 2 decimals [OK]
Common Mistakes:
  • Not using format specifiers correctly
  • Expecting original number without rounding
  • Confusing decimal places with total digits