POOJA GUPTA POOJA GUPTA - 4 months ago 9
Python Question

to count the occurrence of one string in other string in python

If this is the first string :

ABCD


if this is the second string:
ABCD is ABCD


I want to count the occurrence of first string in the second string and that too in python. How can I do that?I'm new to python, so facing some problems. Can anyone tell me the solution or provide the code for the same.

Answer

Use str.count():

>>> str1 = "ABCD"
>>> str2 = "ABCD is ABCD"
>>> str2.count(str1)
2
Comments