CBSE Class 12 Computer Science Question 40 of 120

Review of Python Basics — Question 14

Back to all questions
14
Question

Question 14

What is the output of the following statement?

print("xyyzxyzxzxyy".count('yy', 1))
  1. 2
  2. 0
  3. 1
  4. Error
Answer

2

Reason — The count() method in Python is used to count the occurrences of a substring within a string. In the given code, the string "xyyzxyzxzxyy" is searched for occurrences of the substring 'yy' starting from index 1 (the second position in the string). The substring 'yy' occurs twice in the specified range [1:] of the string "xyyzxyzxzxyy", at indices 1-2 and 10-11. Therefore, the count() method returns 2, indicating that 'yy' occurs twice in the specified range.