Better Way In Python To Count String In Another String
This code works, but reading posts on here I get the impression it is probably not a very 'Pythonic' solution. Is there a better more efficient way to solve this specific problem:
Solution 1:
Why not use the count
method of str
?
>>>a = "abcghabchjlababc">>>a.count("abc")
3
Solution 2:
Another possible solution.
>>>a= 'almforeachalmwhilealmleandroalmalmalm'>>>len(a.split('alm')) - 1
6
>>>q = "abcghabchjlababc">>>len(q.split("abc")) - 1
3
Post a Comment for "Better Way In Python To Count String In Another String"