Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

How might I tally/count the occasions a given substring is available inside a string in Python?

For example:

>>> 'foo bar foo'.numberOfOccurrences('foo')

2

1 Answer

0 votes
by (26.4k points)

You can use string.count(substring), like in:

>>> "abcdabcva".count("ab")

2

Are you interested to learn the concepts of Python? Join the python training course fast!

Related questions

0 votes
1 answer
0 votes
1 answer
+2 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...