How to use the Python count method – with example

  • Reading time:16 mins read

What is Python count?

The Python count method returns the number of times a specified value appears in a string or a list.

 

Using Python count with Strings

We are going to use the count method to determine how many times the letter ‘a’ appears in a string. In this case, we are going to call the string ‘myString’ which will be equal to ‘the cat sat on the mat’.

 

python count

 

We are also going to store the output which is an integer in the variable called ‘t’. The variable ‘t’ will be equal to the name of the string that we intend to perform an operation on and then using the dot operator we append the count method i.e t = myString.count().

Inside the parentheses we can pass three arguments, however in this case the only argument is the letter ‘a’. If we print the variable t, we should get the number of times the letter appears in the string.

 


 

We can also use indexing to determine the actual zone that we are looking for within the string, depending on where we want to search it.

Since Python is a zero-based language the letter ‘t’ in the string ‘the cat sat on the mat’ is going to be represented by index zero. For instance, we can search how many times the letter ‘a’ appears between index 4 and index 10.

 

 

We can also change and search up to index 15 instead of index 10, and the number of times that ‘a’ appears within that index range still remains to be two.

 

 

However, if we change the index up to 10 from 4 we get a zero. This implies that the letter ‘a’ does not appear within that index range.

 

Using Python count with lists

The count method can also be used with lists to return the number of times an element appears in that list. In this section we will create a list with an assortment of random numbers called myList = [1,3,5,2,3,4,5,6,1,2,3,6,7,5].

 

python count

 

Now with lists, we cannot use indexing when performing the count of elements that exist within that list, we can only determine the number of times an item appears in a list.

For instance, if we try to count the number of times the number 14 appears in the above list we will definitely get a zero.

 

 

So let’s also see how many times the number three appears in this list. 

 

 

We can also try the number 4 instead, and in this case, it only appears once in the list.

 

Summary

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.