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’.
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].
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
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to use the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
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.
