How to Use count () Function in Python

# How to Use count () Function in Python

Vikram Singh
Assistant Manager - Content
Updated on Oct 30, 2023 12:38 IST

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.

## 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)Copy code`

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))Copy code```

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'))Copy code```

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']))Copy code```

### 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 = …)Copy code`

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'))Copy code```

Output

Example – 2:

` `
```#use both start and end value in the count function name = 'mahatma gandhi' name.count('a', 2, 10)Copy code```

Output

## 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

Interview Questions