What is the preferred way to count the number of ocurrences of a certain character in a string?
Asked Answered
D

7

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://mcmap.net/q/1861195/-what-is-the-preferred-way-to-count-the-number-of-ocurrences-of-a-certain-character-in-a-string for explanation

Dasilva answered 30/4, 2012 at 15:4 Comment(9)
Sorry I did a mistake with the question, editing now.Dasilva
It is? - even in 3.2 I see no deprecated notes.Overweigh
Did you maybe mean string.count, which is deprecated (alongside most of the rest of the string module)?Kaph
@DanielRoseman That might be it. 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?Dasilva
@Dasilva str and string are different. The former is the type of strings in python, the latter a module that handles string-related things.Overweigh
@Lattyware Should the question be edited to "Why is string.count deprecated / how to replace" or left as it is?Dasilva
@Dasilva 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.Overweigh
Does this answer your question? Count the number occurrences of a character in a stringPoise
@GinoMempin It does answer the headline even though https://mcmap.net/q/1861195/-what-is-the-preferred-way-to-count-the-number-of-ocurrences-of-a-certain-character-in-a-string gives more context related to the deprecation issue. Also funny that an 8 years old question suddenly came to the surfaceDasilva
O
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.

Overweigh answered 30/4, 2012 at 15:11 Comment(2)
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: docs.python.org/library/string.html#deprecated-string-functionsBerberine
@MarkRansom I was going to look at the "String methods" link in the documentation, but somehow forgot :DDasilva
S
5

Use len():

>>> len('abcd')
4
Subdeacon answered 30/4, 2012 at 15:5 Comment(1)
This was the right answer to the question, before the OP edited it.Overweigh
F
2

This works fine in 2.7.3

>>> strs='aabbccddaa'
>>> strs.count('a')
4
Flit answered 30/4, 2012 at 15:13 Comment(0)
C
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
Corm answered 30/4, 2012 at 22:24 Comment(0)
B
1

The other way can be

strs.__len__()

Bartolomeo answered 25/1, 2013 at 3:59 Comment(0)
H
0
yourString = "I have been to Disney"

# Return a list of indexes of spaces in a string
SpaceList = []
# Return the quantity of (spaces in this example It could be any character)
qty = 0
for i in range(len(yourString)):

    if yourString[i] == ' ':
        SpaceList.append(i)
        qty += 1
print(SpaceList)
print(qty)
Hakan answered 15/10 at 19:22 Comment(0)
A
-1
sentence = str(input("Write a Sentence: "))
count = 0
for word in sentence:
    if word == " ":
    count = count + 1
print(count+1)
Actiniform answered 29/4, 2020 at 19:39 Comment(1)
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.Poise

© 2022 - 2024 — McMap. All rights reserved.