Question #6155   Submitted by Answiki on 05/14/2022 at 07:53:23 PM UTC

In Python, how to count the number of occurrences of a character in a string?

Answer   Submitted by Answiki on 05/14/2022 at 07:55:18 PM UTC

In Python, it is possible to count the number of occurrences of a given character with the count() function. Here is an example with the letter c:

string.count('c')

The following example displays 2:

string = "Answiki"
n = string.count('i')
print (n)

Try this example online on repl.it.

3 events in history
Answer by Answiki on 05/14/2022 at 07:55:18 PM

In Python, it is possible to count the number of occurrences of a given character with the count() function. Here is an example with the letter c:

string.count('c')

The following example displays 2:

string = "Answiki"
n = string.count('i')
print (n)

Try this example online on repl.it.

Answer by Answiki on 05/14/2022 at 07:55:05 PM

In Python, it is possible to count the number of occurrences of a given character with the count() function. Here is an example with the letter c:

string.count('c')

The following example displays 2:

string = "Answiki"
n = string.count('i')
print (n)

Try this example online on repl.it.

Question by Answiki 05/14/2022 at 07:53:23 PM
In Python, how to count the number of occurrences of a character in a string?
# ID Query URL Count

Icons proudly provided by Friconix.