- Python List count() Method
- Definition and Usage
- Syntax
- Parameter Values
- More Examples
- Example
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
- Count in Python
- Syntax of count() Function in Python
- Parameters of count() Fucntion in Python
- Return Value of count() Fucntion in Python
- Example of count() Fucntion in Python
- What is the count() Function in Python?
- Application of count() Function in Python
- More Examples
- Example 1: Using Count Method with a Substring.
- Example 2: Count method with Character in a Given Binary String
- Example 3: Count method with Substring in a Given Binary String
- Example 4: Using Start Position Parameters
- Example 5: Using End Position Parameters
- Example 6: Using both Optional Parameters
- Conclusion
- Read More:
- How to Use the Python count() Function
- 1. Python count() function with Strings
- Python String count() method: TypeError
- 2. Python List count() function
- Python count() function at a glance!
- Conclusion
- References
Python List count() Method
Return the number of times the value «cherry» appears in the fruits list:
fruits = [‘apple’, ‘banana’, ‘cherry’]
Definition and Usage
The count() method returns the number of elements with the specified value.
Syntax
Parameter Values
More Examples
Example
Return the number of times the value 9 appears int the list:
points = [1, 4, 2, 9, 7, 8, 9, 3, 1]
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
Count in Python
The count() function of the python is used to count the frequency of the character or a substring in a string. It simply gives the count of occurrences as a return value.
Syntax of count() Function in Python
Python Count() function has following syntax:
Parameters of count() Fucntion in Python
Three parameters are accepted by the count() function of python while using strings:
- substring/character : Which is to be searched in the string.
- start (optional) : The value which is stated as the start position. It is inclusive, which means substring/character at this position is also included in the count. If we leave this parameter empty, then start is considered as 0 th position by default.
- end (optional) : The value stated as the end position. It is exclusive, which means substring/character at this position is excluded from the count. If we leave this parameter empty, then the end is considered the last index of the string by default.
Return Value of count() Fucntion in Python
Return Type: integer
It returns the number of occurrences of substring/character in a string.
Example of count() Fucntion in Python
Let’s use count() function on strings to find the occurrence of any character.
Explanation
- We created a string and stored it into a variable.
- Then, we calculated the occurrence of the character ‘e’ in the string and stored it in the variable.
- Finally, we printed that variable and got the occurrence of ‘e’ in the string.
What is the count() Function in Python?
Let’s say you have a very long binary string(a string that only consists of 0’s and 1’s), and you have a tedious job of counting the number of occurrences of ‘1’ in that string. Have you ever wondered how you would solve this long task within a single second? If you use Python Count() function to find the number of occurrences of ‘1’ in that string, you get desired output within milliseconds.
The python count() function is an inbuilt function of python which is used to count the number of occurrences of a character or substring in the string. Count function is case-sensitive it means that ‘a’ & ‘A’ are not treated as the same.
Application of count() Function in Python
- Count function can find the white spaces in a given string.
- Count function can determine the frequency of any character in a given string.
- Count function can also be used to find out the count of a given word from a string.
Python count() function is used with both string and array/list. When used with array and list, the count() function have a different syntax and parameter but the same return value.
More Examples
Example 1: Using Count Method with a Substring.
Let’s use count() function on strings to find the occurrence of any of its substring(more than one character) in that string.
Explanation
- We created a string and stored it into a variable.
- Then, we calculated the occurrence of substring ‘abcd’ from the string and stored it in the variable.
- Finally, we printed that variable and got the occurrence of ‘abcd’ in the string as an output.
Example 2: Count method with Character in a Given Binary String
Let’s use count() on strings to find the occurrence of any of its character in that binary string.
Explanation
- We created a string and stored it into a variable.
- Then, we calculated the occurrence of character ‘0’ from the string and stored it in the variable.
- Finally, we printed that variable and got the occurrence of ‘0’ in the string as an output.
Example 3: Count method with Substring in a Given Binary String
Let’s use count() on strings to find the occurrence of any of its substring(more than one character) in that string.
Explanation
- We created a string and stored it into a variable.
- Then, we calculated the occurrence of substring ’01’ from string and stored it in the variable.
- Finally, we printed that variable and got the occurrence of ’01’ in the string as an output.
Example 4: Using Start Position Parameters
Let’s use count() on strings to find the occurrence of any of its substring(more than one character) in that string by passing only one optional parameter start , for specifying the start position for finding the substring.
Explanation
- We created a string and stored it into a variable.
- Then, we calculated the occurrence of substring ‘ab’ in a string starting from position 11(inclusive) till the end of the string and stored it in the variable.
- Finally, we printed that variable and got the occurrence of ‘ab’ in the string as an output.
Example 5: Using End Position Parameters
Let’s use count() function on strings to find the occurrence of any of its substring(more than one character) in that string by passing only one optional parameter end for specifying the end position till there we have to find the substring.
While we are only sending a single optional parameter end in count() function. We have to use start parameter as None to specify that we have to start counting from the start of the string.
Explanation
- We created a string and stored it into a variable.
- Then, we calculated the occurrence of substring ‘ab’ in the string from starting of the string till position 15(exclusive) and stored it in the variable.
- Finally, we printed that variable and got the occurrence of ‘ab’ in the string as an output.
Example 6: Using both Optional Parameters
Let’s use count() on strings to find the occurrence of any of its characters in that string by passing some additional optional parameters start & end as discussed earlier.
Explanation
- We created a string and stored it into a variable.
- Then, we calculated the occurrence of character ‘e’ in the string from the position between 3(inclusive) and 14(exclusive) and then stored it in the variable.
- Finally, we printed that variable and got the occurrence of ‘e’ in the string as an output.
Conclusion
- First of all, we understood what count() in python and the basic use of the count function, and we discussed the syntax, parameters, and the return value of the count function.
- The function returns the number of occurrences of provided character/substring in the string.
- We can provide the start and end position of the string to count the occurrences in only a specified portion of the string.
Read More:
How to Use the Python count() Function
Hi, Folks! In this article, we will be focusing on Python count() method with Strings and Lists.
1. Python count() function with Strings
Python String has got an in-built function – string.count() method to count the occurrence of a character or a substring in the particular input string.
The string.count() method accepts a character or a substring as an argument and returns the number of times the input substring happens to appear in the string.
string.count(string, start_index,end_index)
- substring(mandatory) : The string whose occurrence of presence needs to be counted in the input string.
- start_index(optional) : The index from where the search for the substring starts.
- end_index(optional) : The index where the search for the substring needs to stop.
inp_str = "JournalDev -- AskPython @ JournalDev" str_cnt = inp_str.count("JournalDev") print(str_cnt)
inp_str = "Python Java Python Kotlin" str_cnt = inp_str.count("Python", 0 , 6) print(str_cnt)
In the above example, we have passed ‘Python‘ as a substring to be searched and to count for the presence between index 0 – index 6.
inp_str = "Python Java Python Kotlin" str_len=len(inp_str) str_cnt = inp_str.count("Python", 5 , str_len ) print(str_cnt)
Here, we search the substring – ‘Python’ and count its occurrence between index 5 to the end of the string that is why we have passed the length of the string as the end_index argument.
Python String count() method: TypeError
Python string.count() accepts only a single substring as an argument. If we try to pass multiple substrings as arguments, it raises the TypeError exception .
inp_str = "Python Java Python Kotlin" str_cnt = inp_str.count('Python', 'Java') print(str_cnt)
TypeError Traceback (most recent call last) in 1 inp_str = "Python Java Python Kotlin" ----> 2 str_cnt = inp_str.count('Python', 'Java') 3 print(str_cnt) TypeError: slice indices must be integers or None or have an __index__ method
2. Python List count() function
Python list has got a list.count() method to count the occurrence of particular elements in a list.
The list.count() method counts the occurrence of a particular value/ data item present in the input list.
inp_lst = ['Apple','Banana','Apple','Grapes','Jackfruit','Apple'] lst_cnt = inp_lst.count('Apple') print(lst_cnt)
inp_lst = [ ['Rat','Cat'], ['Heat','Beat'], ['Rat','Cat'] ] lst_cnt = inp_lst.count(['Rat','Cat']) print(lst_cnt)
In the above example, we basically count the occurrence of a nested list [‘Rat’, ‘Cat’] inside the list.
Python count() function at a glance!
- Python string.count() function is used to count for the occurrence of the input substring in the particular String.
- The string.count() method raises an TypeError exception , if we try to pass more than one substring as an argument.
- The list.count() function checks for the number of times a particular element occurs in a particular list.
Conclusion
Thus, in this article, we have understood the working of in-built Python count function with Strings and Lists.