8000 Documentation for str.count() should mention the empty string case · Issue #99183 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

Closed
MrHaxtar opened this issue Nov 7, 2022 · 4 comments
Closed

Documentation for str.count() should mention the empty string case #99183

MrHaxtar opened this issue Nov 7, 2022 · 4 comments
Labels
docs Documentation in the Doc dir

Comments

@MrHaxtar
Copy link
MrHaxtar commented Nov 7, 2022

This is the bug in python count() function.

>>> a="I Iove python"
>>> b=a.count("")
>>> print(b)

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

@MrHaxtar MrHaxtar added the type-bug An unexpected behavior, bug, or error label Nov 7, 2022
@hauntsaninja
Copy link
Contributor

This is intentional, you can see that this case is explicitly handled here:

return (str_len < maxcount) ? str_len + 1 : maxcount;

And this preserves behaviour going back at least 22 years, e.g. see:

int count(PyUnicodeObject *self,

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
Copy link
Member

This is the bug in python count() function.

It is not a bug. The empty string matches 14 positions of your string:

  1. An empty string matches before the "I"
  2. And after the "I" and before the first space.
  3. And after the space and before "l".
  4. And between the "l" and the "o".

and so on. If you count them, there are 14 positions where an empty string matches.

See also this Stackoverflow answer.

@stevendaprano stevendaprano changed the title #bug_report:Failed to give error count("") Documentation for str.count() should mention the empty string case Nov 7, 2022
@stevendaprano stevendaprano added docs Documentation in the Doc dir and removed type-bug An unexpected behavior, bug, or error labels Nov 7, 2022
@stevendaprano
Copy link
Member

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
Copy link
Author
MrHaxtar commented Nov 8, 2022

This is the bug in python count() function.

It is not a bug. The empty string matches 14 positions of your string:

  1. An empty string matches before the "I"
  2. And after the "I" and before the first space.
  3. And after the space and before "l".
  4. And between the "l" and the "o".

and so on. If you count them, there are 14 positions where an empty string matches.

See also this Stackoverflow answer.

Thanks for the information

ramvikrams added a commit to ramvikrams/cpython that referenced this issue Nov 9, 2022
Updated the documentation for str.count() and added a example to explain the documentation better.
rhettinger added a commit to rhettinger/cpython that referenced this issue Nov 10, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 10, 2022
…thonGH-99339)

(cherry picked from commit 2f4af2d)

Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 10, 2022
…thonGH-99339)

(cherry picked from commit 2f4af2d)

Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
miss-islington added a commit that referenced this issue Nov 10, 2022
(cherry picked from commit 2f4af2d)

Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
miss-islington added a commit that referenced this issue Nov 10, 2022
(cherry picked from commit 2f4af2d)

Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

4 participants
0