+ 1

Counting the letters

How can I count a specific letter in a word without importing modules..... For example: "Programming" has 2 "m" and 2 "g" and 1"p" in it.... I want to count a specific letter, I want know the code itself, can you help me with the logic please???

7th Feb 2021, 3:59 PM
Adilet Kambarov
Adilet Kambarov - avatar
4 Answers
+ 3
Please give more explaination. If you mean that you want how many times a letter is repeated in a word then go with count() ( exp: word ="hello" print(word.count('l')) >>> 2 If I misunderstood your question please explain with more details.
7th Feb 2021, 4:05 PM
Hisham YUM 🇲🇦
Hisham YUM 🇲🇦 - avatar
+ 6
You can prepare a <sum> variable, initialized to zero. Then run a for-loop on each character in the string, checking whether the character is an alphabet by using `isalpha()` method. When `isalpha()` returns `True`, increment <sum>. You can also use comprehension to generate a `list` or a `tuple` containing alphabets only. Then use `len()` to get the `list` or `tuple` size. You can also use `filter()` to generate a `list` or a `tuple` containing alphabets only. Then use `len()` to get the `list` or `tuple` size. It all depends on how far you had gone through the tutorial. Either way, all those are available in the tutorial.
7th Feb 2021, 4:18 PM
Ipang
+ 4
You can use -> .lenght Of a string, so you can know it's lenght (the letters). Hope I helped you :)
7th Feb 2021, 4:03 PM
Matthew
Matthew - avatar
+ 3
Adilet You can count like this. First check if character exist in dictionary then increment count otherwise store 1 https://code.sololearn.com/cebXGf11Gq58/?ref=app
7th Feb 2021, 5:25 PM
A͢J
A͢J - avatar