How do you find all occurrences of a character in a string python?
I am trying to find all the occurences of "|" in a string. Show
But I get an error:
asked Oct 22, 2012 at 10:39
2
The function:
will return a list of the indices of
Mooncrater 3,5544 gold badges27 silver badges53 bronze badges answered Oct 22, 2012 at 10:50
Marco L.Marco L. 1,4794 gold badges17 silver badges25 bronze badges 3 if you want index of all occurrences of
also you can do
answered Oct 22, 2012 at 10:45
avasalavasal 13.9k4 gold badges30 silver badges47 bronze badges It is easier to use regular expressions here;
answered Oct 22, 2012 at 10:56
Roland SmithRoland Smith 40.8k3 gold badges59 silver badges87 bronze badges
dbr 161k65 gold badges273 silver badges339 bronze badges answered Oct 22, 2012 at 10:57
ZuluZulu 8,0899 gold badges46 silver badges55 bronze badges
I suggest:
answered Oct 22, 2012 at 10:48
samfrancessamfrances 2,9753 gold badges24 silver badges36 bronze badges 1 If
Note: This will also work for searching sub-strings. answered Aug 30, 2018 at 7:26
Pablo ReyesPablo Reyes 2,92919 silver badges29 bronze badges text.find() only returns the first result, and then you need to set the new starting position based on that. So like this:
answered Oct 22, 2012 at 10:48
M SomervilleM Somerville 4,33727 silver badges37 bronze badges 2 How do you find the occurrences of a string in a string Python?The count() method returns the number of occurrences of a substring in the given string.. substring - string whose count is to be found.. start (Optional) - starting index within the string where search starts.. end (Optional) - ending index within the string where search ends.. How do you find the occurrence of all characters in a string?In order to find occurence of each character in a string we can use Map utility of Java.In Map a key could not be duplicate so make each character of string as key of Map and provide initial value corresponding to each key as 1 if this character does not inserted in map before.
How do you find the index of occurrences of a character in a string in Python?5 Ways to Find the Index of a Substring in Strings in Python. str.find(). str.rfind(). str.index(). str.rindex(). re.search(). |