The long string is the length of words in phrases. I have created this long string because I wanted to display it without commas if it was e.g. a tuple. I am counting the number of words which have 3 or 4 letters.
The code is missing 8 occurences of " 2 ". So I wonder, if I understand how the count() works, because if I count the amount of " 2 2 " I am getting also 8. So it looks like, it might be removing first " 2 " from a string leaving there "2 " which I am not counting. Does it work that way?
There just aren’t that many 2s in there. Generally whenever you find yourself questioning the core Python library for something this simple, first consider if you’ve done-goofed and messed up yourself. You probably have.
I did a ctrl + F find all in notepad++. There are only 35 * " 2 " and 2 * " 2."s.
I’m not questioning the core Python library. How did you come to question this? I wonder where is the mistake. And that means it can easily be on my side as well.
But I cannot agree with your calculation. Above I calculated it by hand and it actually gives me 43. When I put it into the online tool it gives me the same number.
So, if there is no error in the doubled 2 (you didn’t confirm that), is the an error in the gap?
>>> help(str.count)
count(...)
S.count(sub[, start[, end]]) -> int
Return the number of non-overlapping occurrences of substring sub in
string S[start:end]. Optional arguments start and end are
interpreted as in slice notation.
Note: non-overlapping. If you look for " 2 " with spaces, then "1 2 2 1" would have overlapping matches.
Thanks, that explains what is the cause of it. Even though I read the help, I haven’t got an idea to look at it again when it didn’t work as expected. I will try to keep it in mind next time.
@Juandev - I wouldn’t feel too bad about that - I’ve been bitten by the issue that standard string search is “non-overlapping” a few times even though I’ve been writing Python for a long time. This is the case both in normal string search and in regex searches.
(There are actually other string search algorithms – other than the standard builtin and regex ones-- that can return overlapping results - if you’d ever want those.)
Of course you can. My point was simply that people sometimes forget that regex search by default also only returns non-overlapping matches. Also, this problem can kind of sneak up on you, even if you are fully aware of the general way regexes work, since the regex could be dynamically constructed and contain partially overlapping alternatives…