Python Question and Answers – Formatting – 1

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Formatting – 1”.

1. What will be the output of the following Python code snippet?

X=”hi”
print(“05d”%X)

a) 00000hi
b) 000hi
c) hi000
d) error
View Answer

Answer: d
Explanation: The code snippet shown above results in an error because the above formatting option works only if ‘X’ is a number. Since in the above case ‘X’ is a string, an error is thrown.

2. What will be the output of the following Python code snippet?

advertisement
X = "san-foundry"
print("%56s" % X)

a) 45 blank spaces before san-foundry
b) 56 blank spaces before san and foundry
c) 56 blank spaces after san-foundry
d) no change
View Answer

Answer: a
Explanation: The formatting option %56s aligns the string to the right within a field of width 56. If the string “san-foundry” is 11 characters long, it will be preceded by (56 – 11 = 45) blank spaces.
Free 30-Day C Certification Bootcamp is Live. Join Now!

3. What will be the output of the following Python code?

x=456
print("%-06d"%x)

a) 000456
b) 456000
c) 456
d) error
View Answer

Answer: c
Explanation: The format specifier %-06d means “left-align the integer in a field of width 6”. The 0 (zero-padding) is ignored when used with -. So, the number 456 is printed left-aligned with spaces on the right, resulting in ‘456 ‘. Visually, it appears as 456.

advertisement

4. What will be the output of the following Python code?

X=345
print(“%06d”%X)

a) 345000
b) 000345
c) 000000345
d) 345000000
View Answer

Answer: b
Explanation: The format specifier %06d means the integer should be printed with a total width of 6 characters, padded with leading zeroes if necessary. Since 345 has 3 digits, it is printed as 000345.

5. Which of the following formatting options can be used in order to add ‘n’ blank spaces after a given string ‘S’?
a) print(“-ns”%S)
b) print(“-ns”%S)
c) print(“%ns”%S)
d) print(“%-ns”%S)
View Answer

Answer: d
Explanation: The format specifier “%-ns” left-aligns the string S in a field of width n. This causes n – len(S) blank spaces to be added after the string to reach the specified width.

6. What will be the output of the following Python code?

X = -122
print("-%06d"%X)

a) −000122
b) 000122
c) −−00122
d) −00122
View Answer

Answer: c
Explanation: The expression “%06d” % X formats -122 as -00122, with leading zeros to make the total width 6. Since there’s an extra – in the string, it gets added as a literal character, resulting in –00122. So the output is –00122.

7. What will be the output of the following Python code?

x=34
print(“%f”%x)

a) 34.00
b) 34.0000
c) 34.000000
d) 34.00000000
View Answer

Answer: c
Explanation: The output of the code is 34.000000. By default, the %f format specifier in Python displays floating-point numbers with 6 digits after the decimal point. Since no precision is explicitly set, x = 34 is printed as 34.000000.

8. What will be the output of the following Python expression?

x=56.236
print("%.2f"%x)

a) 56.00
b) 56.24
c) 56.23
d) 0056.236
View Answer

Answer: b
Explanation: The expression shown above rounds off the given number to the number of decimal places specified. Since the expression given specifies rounding off to two decimal places, the output of this expression will be 56.24. Had the value been x=56.234 (last digit being any number less than 5), the output would have been 56.23.

9. What will be the output of the following Python expression?

x=22.19
print("%5.2f"%x)

a) 22.1900
b) 22.00000
c) 22.19
d) 22.20
View Answer

Answer: c
Explanation: The format “%5.2f” means the number should be printed with 2 digits after the decimal point, and a minimum width of 5 characters (including the decimal point). So 22.19 fits perfectly and is printed as-is.

10. The expression shown below results in an error.

print("-%5d0",989)

a) True
b) False
View Answer

Answer: b
Explanation: The expression shown above does not result in an error. The output of this expression is -%5d0 989. Hence this statement is incorrect.

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.