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
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?
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
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.
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
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.
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
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
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
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
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
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
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
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.
- Check Python Books
- Apply for Programming Internship
- Apply for Python Internship
- Check Information Technology Books
- Practice Programming MCQs