Well, this might be faster since it does the comparing in C:
def occurrences(string, sub):
count = start = 0
while True:
start = string.find(sub, start) + 1
if start > 0:
count+=1
else:
return count
More Related Contents:
- Find substring in string but only if whole words?
- How can I concatenate str and int objects?
- Changing one character in a string
- Random string generation with upper case letters and digits
- Converting a string representation of a list into an actual list object [duplicate]
- How to split the definition of a long string over multiple lines?
- using backslash in python (not to escape)
- Find and replace string values in list
- pandas dataframe str.contains() AND operation
- Pandas Extract Number from String
- Python convert tuple to string
- Finding index of an item closest to the value in a list that’s not entirely sorted
- Python split string into multiple string [duplicate]
- Replace string within file contents [duplicate]
- Splitting a string with repeated characters into a list
- Python string literal concatenation
- Determine precision and scale of particular number in Python
- How can I split a string of a mathematical expressions in python?
- How to return all list elements of a given length?
- How to count digits, letters, spaces for a string in Python?
- Best way to loop over a python string backwards
- Split string at nth occurrence of a given character
- What is internal representation of string in Python 3.x
- Extract string from between quotations
- What does backward-slash b do in Python?
- How to join two string with a new line between them?
- Sort strings by the first N characters
- Python IMAP search using a subject encoded with utf-8
- Why do I get “TypeError: not all arguments converted during string formatting” trying to check for an even/odd number?
- How to pad a string to a fixed length with spaces [duplicate]