How to Use count () Function in Python
The count function is a built-in function in python that returns the number of times a given value occurs in a string or list. In this article, we will learn more about how to use the count function with list and string in python.
The count () function in Python is a handy way to find out how many times something appears in a list or a string. Itβs like quickly counting how many of a certain item you have in a list, which is really useful for keeping track of things easily.
Table of Content
Best-suited Python courses for you
Learn Python with these high-rated online courses
What is the count function in Python?
The count function is a built-in python function that returns the number of times a given value occurs in a string or list. This function can also be used to count the number of elements in the array. It can also find the white spaces in a given string.
There are two different types of count() methods in Python:
- string count () method
- list count () method
Python list count () method:
The list count() method returns the number of times an object appears in a list. It takes a single argument as an input that iterates through the list and counts the number of instances that appear in that specified list.
Syntax
list.count(object)
Parameter
object: the element to be counted.
Examples
Example -1:
#use count function to find the number of times 1729
number = [1191, 1192, 1564, 1729, 1869, 1889, 1729, 1911, 1947, 1729]
print('number of times 1729 is repeated in the list is:', number.count(1729))
Output
Example -2:
#use count function to count the number of times i in the list
name = ['I', 'N', 'F', 'O', 'E', 'D', 'G', 'E']
print ('number of times I is repeated in the list is:', name.count('I'))
Output
Example β 3:
#count list element ['a','e','i','o','u'] inside a list
alphabet = [1, 2, 3, ['a','e','i','o','u'], (7, 8), ['a','e','i','o','u'], 10, 11, ('r', 'v') ]
print ('number of times list is repeated in the list is:', alphabet.count(['a','e','i','o','u']))
Python string count () method:
The string count() function is used to count the frequency of the character or a substring in a string.
- It returns the number of occurrences of substring/character in the string.
- It is a case-sensitive function, i.e., P and p are treated differently (check the below example -1).
Syntax
string.count(substring, start = β¦, end = β¦)
Parameter
- Substring
- It is the character or the substring you want to search in the string
- Start
- It is the position to start the search.
- It is optional and inclusive, i.e., the substring at this position is also included in the count.
- But, if we did not assign any value to this, it will be considered the 0th position by default.
- End
- It is the position to end the search.
- It is optional and exclusive, i.e., the substring or the character at this position is excluded from the count.
- But, if any value is not assigned, it will consider the last index of the string by default.
Examples
Example -1:
#using count function count the number of time p and P appears in Philippines
country = 'Philippines'
print('count of P in Philippines is:', country.count('P'))print('count of p in Philippines is:', country.count('p'))
Output
Example β 2:
#use both start and end value in the count function
name = 'mahatma gandhi'
name.count('a', 2, 10)
Output
Programming Online Courses and Certification | Python Online Courses and Certifications |
Data Science Online Courses and Certifications | Machine Learning Online Courses and Certifications |
Conclusion
In this article, we have discussed how to use count () function in python with list and strings.
Hope you will like the article.
Top Trending Article
Top Online Python Compiler | How to Check if a Python String is Palindrome | Feature Selection Technique | Conditional Statement in Python | How to Find Armstrong Number in Python | Data Types in Python | How to Find Second Occurrence of Sub-String in Python String | For Loop in Python |Prime Number | Inheritance in Python | Validating Password using Python Regex | Python List |Market Basket Analysis in Python | Python Dictionary | Python While Loop | Python Split Function | Rock Paper Scissor Game in Python | Python String | How to Generate Random Number in Python | Python Program to Check Leap Year | Slicing in Python
Interview Questions
Data Science Interview Questions | Machine Learning Interview Questions | Statistics Interview Question | Coding Interview Questions | SQL Interview Questions | SQL Query Interview Questions | Data Engineering Interview Questions | Data Structure Interview Questions | Database Interview Questions | Data Modeling Interview Questions | Deep Learning Interview Questions |
Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio