Kodeclik Logo

Our Programs

Courses

Learn More

Schedule

Kodeclik Blog

Counting letters (but not numbers) in a Python string

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!

Counting just letters and not numbers in a Python string

Method 1: Use a Traditional Loop

The simplest approach is to use a for loop, like so:

sample_string = 'abc123def456ghi789'
letter_count = 0
for char in sample_string:
    if char.isalpha():
        letter_count += 1
print('Letter count using loop:', letter_count)

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:

Letter count using loop: 9

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.

Method 2: Use a List Comprehension

A list comprehension combined with the sum() function provides a more concise and Pythonic solution.

sample_string = 'abc123def456ghi789'
letter_count = sum(1 for char in sample_string if char.isalpha())
print('Letter count using list comprehension:', letter_count)

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:

Letter count using list comprehension: 9

Method 3: Use the filter function

Consider the below code:

sample_string = 'abc123def456ghi789'
letter_count = len(list(filter(str.isalpha, sample_string)))
print('Letter count using filter function:', letter_count)

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:

Letter count using filter function: 9

Method 4: Use a map function

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:

sample_string = 'abc123def456ghi789'
letter_count_map = sum(map(str.isalpha, sample_string))
print('Letter count using map and sum:', letter_count_map)

The output is:

Letter count using map and sum: 9

Method 5: Use a generator expression

Here is yet another approach:

sample_string = 'abc123def456ghi789'
letter_count_gen = sum(i.isalpha() for i in sample_string)
print('Letter count using generator expression:', letter_count_gen)

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:

Letter count using generator expression: 9

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.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

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.

Copyright @ Kodeclik 2024. All rights reserved.