0
0
Pythonprogramming~10 mins

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

Choose your learning style9 modes available
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 ==.