So normally count function is used to check specific word into the string. So if I am using "" it will giving output as 14 instead of showing error. Anyone knows how to handle this bug or may be it's not fixed till now by www.python.org
The text was updated successfully, but these errors were encountered:
In case it helps explain the behaviour, "" is a substring of all strings and e.g. if it's true that "" in string, then it makes sense that string.count("") > 0. While the code today is a little complicated, if you look at my second link from the code 23 years ago, it's pretty easy to understand what count is doing and why it gets 14.
stevendaprano
changed the title
#bug_report:Failed to give error count("")
Documentation for str.count() should mention the empty string case
Nov 7, 2022
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 str.count("") returns one more than the length of the string.
MrHaxtar commentedNov 7, 2022
•
edited by AlexWaygood
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 as14instead 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: