I have a string \"Hello I am going to I with hello am\". I want to find how many times a word occur in the string. Example hello occurs 2 time. I tried this app
Hello I am going to I with hello am
Here is an alternative, case-insensitive, approach
sum(1 for w in s.lower().split() if w == 'Hello'.lower()) 2
It matches by converting the string and target into lower-case.
ps: Takes care of the "am ham".count("am") == 2 problem with str.count() pointed out by @DSM below too :)
"am ham".count("am") == 2
str.count()