Bird
0
0

How can you print the number 7.12345 rounded to 2 decimal places using print() and f-strings?

hard📝 Application Q15 of 15
Python - Input and Output
How can you print the number 7.12345 rounded to 2 decimal places using print() and f-strings?
Aprint(f'{7.12345:2f}')
Bprint(f'{7.12345:.2f}')
Cprint(f'{7.12345:.2}')
Dprint(f'{7.12345:.f2}')
Step-by-Step Solution
Solution:
  1. Step 1: Understand f-string formatting

    To round a float to 2 decimals, use :.2f inside the braces.
  2. Step 2: Check each option

    print(f'{7.12345:.2f}') uses correct syntax {value:.2f}. print(f'{7.12345:2f}') misses the dot before 2. print(f'{7.12345:.2}') uses wrong format specifier. print(f'{7.12345:.f2}') has incorrect order.
  3. Final Answer:

    print(f'{7.12345:.2f}') -> Option B
  4. Quick Check:

    Use .2f to round floats in f-strings [OK]
Quick Trick: Use .2f inside f-string to round floats [OK]
Common Mistakes:
MISTAKES
  • Omitting the dot before precision
  • Using wrong format specifiers
  • Mixing order of letters and numbers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes