Python Quizs Question and Answers – Formatting – 1

Q 1. What will be the output of the following Python expression if x=456?

print(“%-06d”%x)

A. 000456
B. 456000
C. 456
D. error

Show Answer Answer:-C. 456
Explanation The expression shown above results in the output 456.

Q 2. What will be the output of the following Python expression if S=345?

print(“%06d”%S)

A. 345000
B. 000345
C. 000000345
D. 345000000

Show Answer Answer:-B. 000345
Explanation The above expression returns the output 000345. It adds the required number of zeroes before the given number in order to make the number of digits 6 (as specified in this case).

Q 3. 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)

Show Answer Answer:-D. print(“%-ns”%S)
Explanation In order to add ‘n’ blank spaces after a given string ‘S’, we use the formatting option:(“%-ns”%S).

Q 4. What will be the output of the following Python expression if X = -122?

print(“-%06d”%x)

A. -000122
B . 000122
C. –00122
D. -00122

Show Answer Answer:-C. –00122
Explanation The given number is -122. Here the total number of digits (including the negative sign) should be 6 according to the expression. In addition to this, there is a negative sign in the given expression. Hence the output will be – -00122.

Q 5. What will be the output of the following Python expression if the value of x is 34?print(“%f”%x)

A. 34.00
B. 34.0000
C. 34.000000
D. 34.00000000

Show Answer Answer:-C. 34.000000
Explanation The expression shown above normally returns the value with 6 decimal points if it is not specified with any number. Hence the output of this expression will be: 34.000000 (6 decimal points).

Q 6. What will be the output of the following Python expression if x=56.236?

print(“%.2f”%x)

A. 56.00
B. 56.24
C. 56.23
D. 0056.236

Show Answer Answer:-B. 56.24
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.

Q 7. What will be the output of the following Python expression if x=22.19?

print(“%5.2f”%x)

A. 22.1900
B. 22.00000
C. 22.19
D. 22.20

Show Answer Answer:-C. 22.19
Explanation The output of the expression above will be 22.19. This expression specifies that the total number of digits (including the decimal point) should be 5, rounded off to two decimal places.

Q 8. The expression shown below results in an error.
print(“-%5d0”,988)

A. True
B. False

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

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

S=”hi”

print(“05d”%S)

A. 00000hi
B. 000hi
C. hi000
D. error

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

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

X=”san-foundry”

print(“%56s”,X)

A. 56 blank spaces before san-foundry
B. 56 blank spaces before san and foundry
C. 56 blank spaces after san-foundry
D. no change

Show Answer Answer:-A. 56 blank spaces before san-foundry
Explanation The formatting option print(“%Ns”,X) helps us add ‘N’ number of spaces before a given string ‘X’. Hence the output for the code snippet shown above will be 56 blank spaces before the string “san-foundry”.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page