Python Multiple Choice Questions – String

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

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

  1. >>>"a"+"bc"

a) a
b) bc
c) bca
d) abc
View Answer

Answer: d
Explanation: + operator is concatenation operator.
advertisement
advertisement

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

  1. >>>"abcd"[2:]

a) a
b) ab
c) cd
d) dc
View Answer

Answer: c
Explanation: Slice operation is performed on string.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. The output of executing string.ascii_letters can also be achieved by:
a) string.ascii_lowercase_string.digits
b) string.ascii_lowercase+string.ascii_uppercase
c) string.letters
d) string.lowercase_string.uppercase
View Answer

Answer: b
Explanation: Execute in shell and check.
advertisement

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

  1. >>> str1 = 'hello'
  2. >>> str2 = ','
  3. >>> str3 = 'world'
  4. >>> str1[-1:]

a) olleh
b) hello
c) h
d) o
View Answer

Answer: d
Explanation: -1 corresponds to the last index.
advertisement

5. What arithmetic operators cannot be used with strings?
a) +
b) *
c) –
d) All of the mentioned
View Answer

Answer: c
Explanation: + is used to concatenate and * is used to multiply strings.

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

  1. >>>print (r"\nhello")

a) a new line and hello
b) \nhello
c) the letter r and then hello
d) error
View Answer

Answer: b
Explanation: When prefixed with the letter ‘r’ or ‘R’ a string literal becomes a raw string and the escape sequences such as \n are not converted.

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

  1. >>>print('new' 'line')

a) Error
b) Output equivalent to print ‘new\nline’
c) newline
d) new line
View Answer

Answer: c
Explanation: String literal separated by whitespace are allowed. They are concatenated.

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

  1. >>> print('x\97\x98')

a) Error
b)

   97
   98

c) x\97
d) \x97\x98
View Answer

Answer: c
Explanation: \x is an escape sequence that means the following 2 digits are a hexadecimal number encoding a character.

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

  1. >>>str1="helloworld"
  2. >>>str1[::-1]

a) dlrowolleh
b) hello
c) world
d) helloworld
View Answer

Answer: a
Explanation: Execute in shell to verify.

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

print(0xA + 0xB + 0xC)

a) 0xA0xB0xC
b) Error
c) 0x22
d) 33
View Answer

Answer: d
Explanation: 0xA and 0xB and 0xC are hexadecimal integer literals representing the decimal values 10, 11 and 12 respectively. There sum is 33.

More MCQs on String in Python:

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.