Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (12.7k 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
0 votes
1 answer
asked Nov 13, 2019 in Java by Nigam (4k points)
0 votes
1 answer

Browse Categories

...