Count Number of Characters in List of Strings in Python (2 Examples)

 

This article illustrates how to count the lengths of strings in a list in the Python programming language.

Table of contents:

So now the part you have been waiting for – the examples!

 

Introduction of Example Data

We’ll use the following data as a basis for this Python tutorial:

strings = ["apple", "banana", "cherry", "date"]    # create sample string list
 
print(strings)                                     # print strings list
# ['apple', 'banana', 'cherry', 'date']

As seen, we have created a sample list called strings, which contains four different strings of different lengths. Let’s see the first example of counting the number of characters in strings!

 

Example 1: Find Length of Strings using List Comprehension

In this example, we will use list comprehension to count the length of each string in the strings list.

lengths = [len(s) for s in strings]                # calculate list of lengths
 
print(lengths)                                     # print list of lengths
# [5, 6, 6, 4]

Have a look at the previous output of the Python console. For each item/string s in the strings list, the len function is employed to calculate the length of the string, or in other words, the count of the characters within the string.

As you can see, the count is returned as 5, 6, 6, and 4. You can count the characters manually and confirm the result. Let’s see another example next!

 

Example 2: Find Length of Strings using map Function

Differently, in this example, we will use the map function to apply the len function to every item/string in the strings list.

lengths = list(map(len, strings))                  # calculate list of lengths
 
print(lengths)                                     # print list of lengths
# [5, 6, 6, 4]

Here, we get the same result as expected. You can choose the best method for you!

 

Video & Further Resources

I have recently published a video on my YouTube channel, which demonstrates the content of this article. Please find the video below.

 

The YouTube video will be added soon.

 

In addition to the video, you may read the related articles on this homepage. Some tutorials on topics such as counting, numeric values, and lists can be found here:

 

You have learned in this tutorial how to find the count of characters in a list in the Python programming language. Please tell me about it in the comments section below, if you have additional questions.

 

Cansu Kebabci R Programmer & Data Scientist

This page was created in collaboration with Cansu Kebabci. Have a look at Cansu’s author page to get more information about her professional background, a list of all his tutorials, as well as an overview on her other tasks on Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top