Python MCQ – String – 8

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

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

print("Hello {name1} and {name2}".format(name1='foo', name2='bin'))

a) Hello foo and bin
b) Hello {name1} and {name2}
c) Error
d) Hello and
View Answer

Answer: a
Explanation: The arguments are accessed by their names.
advertisement
advertisement

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

print("Hello {0!r} and {0!s}".format('foo', 'bin'))

a) Hello foo and foo
b) Hello ‘foo’ and foo
c) Hello foo and ‘bin’
d) Error
View Answer

Answer: b
Explanation: !r causes the characters ‘ or ” to be printed as well.

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

advertisement
print("Hello {0} and {1}".format(('foo', 'bin')))

a) Hello foo and bin
b) Hello (‘foo’, ‘bin’) and (‘foo’, ‘bin’)
c) Error
d) None of the mentioned
View Answer

Answer: c
Explanation: IndexError, the tuple index is out of range.
advertisement

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

print("Hello {0[0]} and {0[1]}".format(('foo', 'bin')))

a) Hello foo and bin
b) Hello (‘foo’, ‘bin’) and (‘foo’, ‘bin’)
c) Error
d) None of the mentioned
View Answer

Answer: a
Explanation: The elements of the tuple are accessed by their indices.

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

print('The sum of {0} and {1} is {2}'.format(2, 10, 12))

a) The sum of 2 and 10 is 12
b) Error
c) The sum of 0 and 1 is 2
d) None of the mentioned
View Answer

Answer: a
Explanation: The arguments passed to the function format can be integers also.

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

print('The sum of {0:b} and {1:x} is {2:o}'.format(2, 10, 12))

a) The sum of 2 and 10 is 12
b) The sum of 10 and a is 14
c) The sum of 10 and a is c
d) Error
View Answer

Answer: b
Explanation: 2 is converted to binary, 10 to hexadecimal and 12 to octal.

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

print('{:,}'.format(1112223334))

a) 1,112,223,334
b) 111,222,333,4
c) 1112223334
d) Error
View Answer

Answer: a
Explanation: A comma is added after every third digit from the right.

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

print('{:,}'.format('1112223334'))

a) 1,112,223,334
b) 111,222,333,4
c) 1112223334
d) Error
View Answer

Answer: d
Explanation: An integer is expected.

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

print('{:$}'.format(1112223334))

a) 1,112,223,334
b) 111,222,333,4
c) 1112223334
d) Error
View Answer

Answer: d
Explanation: $ is an invalid format code.

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

print('{:#}'.format(1112223334))

a) 1,112,223,334
b) 111,222,333,4
c) 1112223334
d) Error
View Answer

Answer: c
Explanation: The number is printed as it is.

Sanfoundry Global Education & Learning Series – Python.

To practice all multiple choice questions on Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.