Python MCQ – String – 7

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

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

print("ab\tcd\tef".expandtabs())

a) ab  cd  ef
b) abcdef
c) ab\tcd\tef
d) ab cd ef
View Answer

Answer: a
Explanation: Each \t is converted to 8 blank spaces by default.

advertisement
advertisement

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

print("ab\tcd\tef".expandtabs(4))

a) ab   cd   ef
b) abcdef
c) ab\tcd\tef
d) ab cd ef
View Answer

Answer: d
Explanation: Each \t is converted to 4 blank spaces.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

advertisement
print("ab\tcd\tef".expandtabs('+'))

a) ab+cd+ef
b) ab++++++++cd++++++++ef
c) ab cd ef
d) none of the mentioned
View Answer

Answer: d
Explanation: TypeError, an integer should be passed as an argument.

advertisement

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

print("abcdef".find("cd") == "cd" in "abcdef")

a) True
b) False
c) Error
d) None of the mentioned
View Answer

Answer: b
Explanation: The function find() returns the position of the sunstring in the given string whereas the in keyword returns a value of Boolean type.

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

print("abcdef".find("cd"))

a) True
b) 2
c) 3
d) None of the mentioned
View Answer

Answer: b
Explanation: The first position in the given string at which the substring can be found is returned.

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

print("ccdcddcd".find("c"))

a) 4
b) 0
c) Error
d) True
View Answer

Answer: b
Explanation: The first position in the given string at which the substring can be found is returned.

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

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

a) Hello foo and bin
b) Hello {0} and {1} foo bin
c) Error
d) Hello 0 and 1
View Answer

Answer: a
Explanation: The numbers 0 and 1 represent the position at which the strings are present.

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

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

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

Answer: a
Explanation: The numbers 0 and 1 represent the position at which the strings are present.

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

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

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

Answer: a
Explanation: It is the same as Hello {0} and {1}.

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

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

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

Answer: c
Explanation: The arguments passed to the function format aren’t keyword arguments.

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of 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.