Computer Science
What is the output of the following statement?
python print("xyyzxyzxzxyy".count('yy', 1))
- 2
- 0
- 1
- 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
Which arithmetic operator(s) cannot be used with strings ?
- +
- *
- -
- All of these
What will be the output when the following code is executed?
>>> strl = "helloworld" >>> strl[ : : -1]
- dlrowolleh
- hello
- world
- helloworld
Suppose list1 = [0.5 * x for x in range(0, 4)], list1 is:
- [0, 1, 2, 3]
- [0, 1, 2, 3, 4]
- [0.0, 0.5, 1.0, 1.5]
- [0.0, 0.5, 1.0, 1.5, 2.0]
Which is the correct form of declaration of dictionary ?
- Day = {1:'Monday', 2:'Tuesday', 3:'wednesday'}
- Day = {1;'Monday', 2;'Tuesday', 3;'wednesday'}
- Day = [1:'Monday', 2:'Tuesday', 3:'wednesday']
- Day = {1'monday', 2'tuesday', 3'wednesday'}