close

python find all occurrences in string

Hi guys. How are you all? I hope you all fine. Today in this tutorial we will learn about python find all occurrences in string. Here i am giving you some method tp find all occurrences in string. So without wasting your time lets learn about of these methods. It is very simple and easy methods. Lets learn about of this:

python find all occurrences in string

  1. python find all occurrences in string

    to find all occurrences in string just Use string.count(). You can use string.count() to find all occurrences in string. Here you can find the number of occurrences of a substring in a given particular string. For that you have to make your string first of all. And then mention a substring you want to find from your string and then use this function and print the counts of substring. Here you can also find the starting positions of these substrings. So lets learn this by given below example:
    mystr = "Do good. Be good" substr = "good" count1 = mystr.count(substr) print(count1) count2 = mystr.count(substr,0,10) print(count2) Output : 2 1

  2. How to find all occurrences in string

    to find all occurrences in string just Use startswith(). By using srartswith() you can find all occurrences in string. For that first of all make your string and then substring you want to find out from your string then use startswith function and it will give you the start indices of the sunstrings. Lets learn this how it works by given below example: mystr = "Do good. Be good" substr = "good" print("The original string is : " + mystr) print("Want to find the substring is : " + substr) var1 = [i for i in range(len(mystr)) if mystr.startswith(substr, i)] print("The start indices of the substrings are : " + str(var1)) Output : The original string is : Do good. Be good Want to find the substring is : good The start indices of the substrings are : [3, 12]

Method 1: Use string.count()

You can use string.count() to find all occurrences in string. Here you can find the number of occurrences of a substring in a given particular string. For that you have to make your string first of all. And then mention a substring you want to find from your string and then use this function and print the counts of substring. Here you can also find the starting positions of these substrings. So lets learn this by given below example:

mystr = "Do good. Be good"
substr = "good"
count1 = mystr.count(substr)
print(count1)
count2 = mystr.count(substr,0,10)
print(count2)

Output :

2
1

Method 2: Use startswith()

By using srartswith() you can find all occurrences in string. For that first of all make your string and then substring you want to find out from your string then use startswith function and it will give you the start indices of the sunstrings. Lets learn this how it works by given below example:

mystr = "Do good. Be good"
substr = "good"
print("The original string is : " + mystr)
print("Want to find the substring is : " + substr)
var1 = [i for i in range(len(mystr)) if mystr.startswith(substr, i)]
print("The start indices of the substrings are : " + str(var1))

Output :

The original string is : Do good. Be good
Want to find the substring is : good
The start indices of the substrings are : [3, 12]

Method 3: Use refinditer()

You can use refinditer() to fnd all the occurrences in the string. Here first of all you have to import re and the make your string and substring you want to find out from the string and then use this function. As the above methods it will give you the indices of the substrings. Lets learn this by given below example:

import re  
mystr = "Do good. Be good"
substr = "good"
print("The original string is: " + mystr) 
print("Want to find the substring is: " + substr) 
result = [_.start() for _ in re.finditer(substr, mystr)] 
print("The start indices of the substrings are : " + str(result))

Output :

The original string is: Do good. Be good
Want to find the substring is: good
The start indices of the substrings are : [3, 12]

Conclusion

Hope all 6 Method Are Useful For You. Comment Below Which Method You Used To Convert String To Datetime In Python. Thank You.

Also Read

Leave a Comment