LearnFrenzy
Sign in Take a free test

Strings · Predict the Output · Question 6

find(), rfind() and count() Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

What is the output of the following Python program?

main.py
s = "banana"
print(s.find("an"), s.rfind("an"), s.find("z"), s.count("a"), s.count("ana"))
  • A 1 3 -1 3 2
  • B 1 4 -1 3 1
  • C 1 3 -1 3 1 Correct
  • D 2 4 None 3 1
Correct Answer: Option C

"an" first appears at index 1 and last at index 3. count() is non-overlapping, so "ana" is counted once (the second would overlap the first).

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links