Skip to main content
Shashank RK

1 week ago

Basic Python

This is regarding the string search in a word , below is the example:


'shashank'.find('s')
 it would display the first index in the string , however it would not display the other 's' which is in index 3. how do we do that in case i need to find all occurenece.

Is it only that "find" attribute gets a single word.

Image of
1 week ago
There are diffrent solutions to this but you can call find again to search from the index of s that you have found to the end of the string and so on untill you have reached the end of the string.
Image of
1 week ago
Ermin is correct, the only way to do what you are suggesting is to run find again.  Keep track of the index value of the found occurrence and then find takes an optional start index, so in this case, you could tell it to start at index 1.
Image of
1 week ago
I misspoke in my response, it is not the only way, but using find it is the only way.

Image of Shashank RK
Shashank RK
1 week ago
Thank you Ermin and Larry for your inputs