Count the number of occurrences of a character in a string
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Cosmic Puzzle
--
Chapters
00:00 Question
00:22 Accepted answer (Score 1692)
00:45 Answer 2 (Score 184)
00:58 Answer 3 (Score 137)
01:15 Answer 4 (Score 63)
01:28 Thank you
--
Full question
https://stackoverflow.com/questions/1155...
Accepted answer links:
[str.count(sub[, start[, end]])]: https://docs.python.org/3/library/stdtyp...
Answer 2 links:
[.count()]: https://docs.python.org/2/library/string...
Answer 3 links:
[collections.Counter]: https://docs.python.org/2/library/collec...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #string #count
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Cosmic Puzzle
--
Chapters
00:00 Question
00:22 Accepted answer (Score 1692)
00:45 Answer 2 (Score 184)
00:58 Answer 3 (Score 137)
01:15 Answer 4 (Score 63)
01:28 Thank you
--
Full question
https://stackoverflow.com/questions/1155...
Accepted answer links:
[str.count(sub[, start[, end]])]: https://docs.python.org/3/library/stdtyp...
Answer 2 links:
[.count()]: https://docs.python.org/2/library/string...
Answer 3 links:
[collections.Counter]: https://docs.python.org/2/library/collec...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #string #count
#avk47
ACCEPTED ANSWER
Score 1814
str.count(sub[, start[, end]])Return the number of non-overlapping occurrences of substring
subin the range[start, end]. Optional argumentsstartandendare interpreted as in slice notation.
>>> sentence = 'Mary had a little lamb'
>>> sentence.count('a')
4
ANSWER 2
Score 197
You can use .count() :
>>> 'Mary had a little lamb'.count('a')
4
ANSWER 3
Score 61
Regular expressions maybe?
import re
my_string = "Mary had a little lamb"
len(re.findall("a", my_string))
ANSWER 4
Score 37
Python-3.x:
"aabc".count("a")
str.count(sub[, start[, end]])
Return 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.