250+ TOP MCQs on Python Strings and Answers

Python Quiz on “Strings”.

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

print("xyyzxyzxzxyy".count('yy'))

a) 2
b) 0
c) error
d) none of the mentioned
Answer: a
Clarification: Counts the number of times the substring ‘yy’ is present in the given string.

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

print("xyyzxyzxzxyy".count('yy', 1))

a) 2
b) 0
c) 1
d) none of the mentioned
Answer: a
Clarification: Counts the number of times the substring ‘yy’ is present in the given string, starting from position 1.

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

print("xyyzxyzxzxyy".count('yy', 2))

a) 2
b) 0
c) 1
d) none of the mentioned
Answer: c
Clarification: Counts the number of times the substring ‘yy’ is present in the given string, starting from position 2.

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

print("xyyzxyzxzxyy".count('xyy', 0, 100))

a) 2
b) 0
c) 1
d) error
Answer: a
Clarification: An error will not occur if the end value is greater than the length of the string itself.

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

print("xyyzxyzxzxyy".count('xyy', 2, 11))

a) 2
b) 0
c) 1
d) error
Answer: b
Clarification: Counts the number of times the substring ‘xyy’ is present in the given string, starting from position 2 and ending at position 11.

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

print("xyyzxyzxzxyy".count('xyy', -10, -1))

a) 2
b) 0
c) 1
d) error

Answer: b
Clarification: Counts the number of times the substring ‘xyy’ is present in the given string, starting from position 2 and ending at position 11.

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

a) abc
b) ‘abc’
c) b’abc’
d) h’abc’
Answer: c
Clarification: A bytes object is returned by encode.

8. What is the default value of encoding in encode()?
a) ascii
b) qwerty
c) utf-8
d) utf-16
Answer: c
Clarification: The default value of encoding is utf-8.

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

print("xyyzxyzxzxyy".endswith("xyy"))

a) 1
b) True
c) 3
d) 2
Answer: b
Clarification: The function returns True if the given string ends with the specified substring.

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

print("xyyzxyzxzxyy".endswith("xyy", 0, 2))

a) 0
b) 1
c) True
d) False
Answer: d
Clarification: The function returns False if the given string does not end with the specified substring.

quiz questions on Python,

Leave a Comment

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

Scroll to Top