About
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.
Popular Classes
Scratch Coding
Minecraft Coding
TinkerCAD
Roblox Studio
Python for Kids
Javascript for Kids
Pre-Algebra
Geometry for Kids
Copyright @ Kodeclik 2025. All rights reserved.
Consider a Python string such as “abc123def456ghi789”. Suppose I wish to count the number of letters in it (in this case it is 9). How can we write a Python program to do this for us? Let us count the ways!
The simplest approach is to use a for loop, like so:
This method uses a traditional for loop with the isalpha() function to check each character. It's the most straightforward and readable approach, making it ideal for beginners or when code clarity is paramount.
The output is:
This method is also advantageous when you need to perform additional operations on each character during iteration, as you can easily add more conditions or processing steps within the loop.
A list comprehension combined with the sum() function provides a more concise and Pythonic solution.
This method is particularly useful when you want to write more elegant code and when performance is important for smaller strings. It's also beneficial when you're working with functional programming patterns in Python.
The output is:
Consider the below code:
The filter() function approach is particularly useful when you might need to reuse the filtered sequence for other operations, as you can store the filter object before counting. The output is:
The map function approach is memory-efficient as it doesn't create an intermediate list, making it suitable for processing large strings. This is how it works:
The output is:
Here is yet another approach:
The generator expression method is memory-efficient like the map approach, but with a more readable syntax. It's particularly useful when working with large strings as it processes one character at a time without storing the entire sequence in memory. The output is again:
Have you noticed the common theme in all the above methods? Yes, it is the “isalpha()” method which is really doing the heavy lifting in all the programs. How this method is used to test all given characters in a string (and aggregate the results) makes up the different approaches.
isalpha() is a method that can be applied to either individual characters or to strings. This method tests if the given character is an alphabetic character, and thus a letter.When applied to a string, isalpha() checks every character in that string and returns True only if all characters in the string are letters and False otherwise.
We hope you enjoyed learning how to count letters in a Python string. Which of the above approaches is your favorite?
Want to learn Python with us? Sign up for 1:1 or small group classes.
sample_string = 'abc123def456ghi789'
letter_count = 0
for char in sample_string:
if char.isalpha():
letter_count += 1
print('Letter count using loop:', letter_count)Letter count using loop: 9sample_string = 'abc123def456ghi789'
letter_count = sum(1 for char in sample_string if char.isalpha())
print('Letter count using list comprehension:', letter_count)Letter count using list comprehension: 9sample_string = 'abc123def456ghi789'
letter_count = len(list(filter(str.isalpha, sample_string)))
print('Letter count using filter function:', letter_count)Letter count using filter function: 9sample_string = 'abc123def456ghi789'
letter_count_map = sum(map(str.isalpha, sample_string))
print('Letter count using map and sum:', letter_count_map)Letter count using map and sum: 9sample_string = 'abc123def456ghi789'
letter_count_gen = sum(i.isalpha() for i in sample_string)
print('Letter count using generator expression:', letter_count_gen)Letter count using generator expression: 9