-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Documentation for str.count() should mention the empty string case #99183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is intentional, you can see that this case is explicitly handled here: cpython/Objects/stringlib/count.h Line 22 in df3a6d9
And this preserves behaviour going back at least 22 years, e.g. see: cpython/Objects/unicodeobject.c Line 1864 in d57fd91
In case it helps explain the behaviour, "" is a substring of all strings and e.g. if it's true that |
It is not a bug. The empty string matches 14 positions of your string:
and so on. If you count them, there are 14 positions where an empty string matches. See also this Stackoverflow answer. |
count("")
This issue comes up fairly regularly and many people seem to be surprised by it. I think it might help for the docs to explicitly mention that the empty string matches every position in the string, and so |
Thanks for the information |
Updated the documentation for str.count() and added a example to explain the documentation better.
…thonGH-99339) (cherry picked from commit 2f4af2d) Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
…thonGH-99339) (cherry picked from commit 2f4af2d) Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
Uh oh!
There was an error while loading. Please reload this page.
This is the bug in python count() function.
So normally count function is used to check specific word into the string. So if I am using
""
it will giving output as14
instead of showing error. Anyone knows how to handle this bug or may be it's not fixed till now by www.python.orgThe text was updated successfully, but these errors were encountered: