KnowledgeBoat Logo

Computer Science

What is the output of the following statement?

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

  1. 2
  2. 0
  3. 1
  4. Error

Python String Manipulation

1 Like

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.

Answered By

1 Like


Related Questions