5

How do I do this without string.count(), because it is listed as deprecated in Python v2.7.3 documentation?

I am unable to find what I should use instead.

EDIT: The question originally stated that str.count() was deprecated but that is incorrect and misleading. See https://stackoverflow.com/a/10385996/966508 for explanation

varesa
  • 2,399
  • 7
  • 26
  • 45
  • Sorry I did a mistake with the question, editing now. – varesa Apr 30 '12 at 15:06
  • [It is?](http://docs.python.org/library/stdtypes.html#str.count) - even in [3.2](http://docs.python.org/py3k/library/stdtypes.html#str.count) I see no deprecated notes. – Gareth Latty Apr 30 '12 at 15:09
  • 3
    Did you maybe mean `string.count`, which is deprecated (alongside most of the rest of the `string` module)? – Daniel Roseman Apr 30 '12 at 15:12
  • @DanielRoseman That might be it. http://docs.python.org/library/string.html lists it under "deprecated string functions". But if it were the whole module, why aren't they all deprecated? – varesa Apr 30 '12 at 15:16
  • 2
    @varesa `str` and `string` are different. The former is the type of strings in python, the latter a module that handles string-related things. – Gareth Latty Apr 30 '12 at 15:17
  • @Lattyware Should the question be edited to "Why is string.count deprecated / how to replace" or left as it is? – varesa Apr 30 '12 at 15:23
  • @varesa I think as it stands it's fine, this way at least if someone searches for this, it might come up so they get the explanation. – Gareth Latty Apr 30 '12 at 15:24
  • Does this answer your question? [Count the number occurrences of a character in a string](https://stackoverflow.com/questions/1155617/count-the-number-occurrences-of-a-character-in-a-string) – Gino Mempin May 02 '20 at 01:40
  • @GinoMempin It does answer the headline even though https://stackoverflow.com/a/10385996/966508 gives more context related to the deprecation issue. Also funny that an 8 years old question suddenly came to the surface – varesa May 03 '20 at 21:17

6 Answers6

17

Use str.count() - it's not listed as deprecated.

(Python 2.7.3, Python 3.2.3 - both have no notes about being deprecated).

>>> "test".count("t")
2

I'll presume you meant string.count() - which is depreciated in favour of the method on string objects.

The difference is that str.count() is a method on string objects, while string.count() is a function in the string module.

>>> "test".count("t")
2
>>> import string
>>> string.count("test", "t")
2

It's clear why the latter has been deprecated (and removed in 3.x) in favour of the former.

Gareth Latty
  • 86,389
  • 17
  • 178
  • 183
  • The first time I ran into the "deprecated" designation I was confused too. You need to pay attention to the advice to use string methods instead: http://docs.python.org/library/string.html#deprecated-string-functions – Mark Ransom Apr 30 '12 at 15:19
  • @MarkRansom I was going to look at the "String methods" link in the documentation, but somehow forgot :D – varesa Apr 30 '12 at 15:21
5

Use len():

>>> len('abcd')
4
Gareth Latty
  • 86,389
  • 17
  • 178
  • 183
grifaton
  • 3,986
  • 4
  • 30
  • 42
2

This works fine in 2.7.3

>>> strs='aabbccddaa'
>>> strs.count('a')
4
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
1

Without using count you can do this:

def my_count(my_string, key_char):
    return sum(c == key_char for c in my_string)

Result:

>>> my_count('acavddgaaa','a')
5
Akavall
  • 82,592
  • 51
  • 207
  • 251
1

The other way can be

strs.__len__()

Mirage
  • 30,868
  • 62
  • 166
  • 261
-1
sentence = str(input("Write a Sentence: "))
count = 0
for word in sentence:
    if word == " ":
    count = count + 1
print(count+1)
Joffrey Schmitz
  • 2,393
  • 3
  • 19
  • 28
  • 2
    1) This counts the number of spaces, but the OP was asking for "*count number of ocurrances of certain character*", 2) Technically, it's `for letter in sentence`. – Gino Mempin Apr 29 '20 at 23:53