Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The
format() method is used to insert variables into strings where curly braces {} are placed.2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong number for decimal places.
Forgetting the 'f' after the number.
✗ Incorrect
Inside the curly braces,
.2f formats the number to 2 decimal places.3fill in blank
hardFix 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'
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.
✗ Incorrect
The correct method to insert multiple variables into a string is
format().4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same alignment for both fields.
Using center alignment
^ instead of left or right.✗ Incorrect
Use
< to align left and > to align right inside the format specifier.5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
k.lower() instead of uppercasing keys.Using wrong comparison operators like
< or ==.✗ Incorrect
Use
k.upper() to uppercase keys, v for values, and > to filter values greater than 10.