close

How to find all occurrences in string in Python

Hello guys. How are you all? I hope you all fine. In this tutorial we will learn about how to find all occurrences in string in python. so without wasting time lets learn about of this.

How to find all occurrences in string in Python

  1. python find all occurrences in string

    To find all occurrences in string use startswith(). By using startswith() you can find all occurrences in string in python. So lets learn about of this without wasting time by given below example: mystr = "I love dogs. dogs are loyal" substr = "dogs" print("my string is : " + mystr) print("substring : " + substr) res = [i for i in range(len(mystr)) if mystr.startswith(substr, i)] print("The start indices of the substrings are : " + str(res)) Output : my string is : I love dogs. dogs are loyal substring : dogs The start indices of the substrings are : [7, 13]

  2. How to find all occurrences in string in Python

    To find all occurrences in string use string.count(). By using string.count() you can find all occurrences in string in python. So lets learn about of this without wasting time by given below example: mystr = "I love dogs. dogs are loyal" substr = "dogs" count1 = mystr.count(substr) print(count1) count2 = mystr.count(substr,0,12) print(count2) Output : 2 1

  3. python find all occurrences in string

    To find all occurrences in string use startswith(). By using startswith() you can find all occurrences in string in python. So lets learn about of this without wasting time by given below example: mystr = "I love dogs. dogs are loyal" substr = "dogs" print("my string is : " + mystr) print("substring : " + substr) res = [i for i in range(len(mystr)) if mystr.startswith(substr, i)] print("The start indices of the substrings are : " + str(res)) Output : my string is : I love dogs. dogs are loyal substring : dogs The start indices of the substrings are : [7, 13]

Method 1: find all occurrences in python Using startswith()

By using startswith() you can find all occurrences. So lets learn about of this without wasting time by given below example:

mystr = "I love dogs. dogs are loyal"
substr = "dogs"
print("my string is : " + mystr)
print("substring : " + substr)
res = [i for i in range(len(mystr)) if mystr.startswith(substr, i)]
print("The start indices of the substrings are : " + str(res))

Output :

my string is : I love dogs. dogs are loyal
substring : dogs
The start indices of the substrings are : [7, 13]

Method 2: Use string.count()

By using string.count() you can find all occurrences. So lets learn about of this without wasting time by given below example:

mystr = "I love dogs. dogs are loyal"
substr = "dogs"
count1 = mystr.count(substr)
print(count1)
count2 = mystr.count(substr,0,12)
print(count2)

Output :

2
1

Conclusion

It’s all About this Tutorial. Hope all methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you?

Also, Read

Leave a Comment