0 votes
1 view
in Python by (47.8k points)

What's the simplest way to count the number of occurrences of a character in a string?

e.g. 

count the number of times 'a' appears in 'Mary had a little lamb'

1 Answer

0 votes
by (107k points)

In Python, we have a function str.count() which tells the number of count of any character in a string:-

str.count(sub[, start[, end]]):-

It returns the number of non-overlapping occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.

Below is an example that shows you how it works:-

sentence = 'Mary had a little lamb'

sentence.count('a') 

image

 

 

 

 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
0 votes
1 answer
asked Sep 26, 2019 in Python by Sammy (47.8k points)
0 votes
1 answer
asked Sep 10, 2019 in Python by Sammy (47.8k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...