In this Python tutorial, you will learn:

Python count
The syntax for PythonString Count()
Example 1: Count Method on a String
Example 2: Count occurrence of a character in a given string
Example 3: Count occurrence of substring in a given string

The syntax for PythonString Count()

Python count function syntax:

Parameters of Python Syntax

Char or substring: You can specify a single character or substring you are wants to search in the given string. It will return you the count of the character or substring in the given string. start : (optional) It indicates the start index from where the search will begin. If not given, it will start from 0. For example, you want to search for a character from the middle of the string. You can give the start value to your count function. end: (optional) It indicates the end index where the search ends. If not given, it will search till the end of the list or string given. For example, you don’t want to scan the entire string and limit the search till a specific point you can give the value to end in your count function, and the count will take care of searching till that point.

ReturnValue

The count() method will return an integer value, i.e., the count of the given element from the given string. It returns a 0 if the value is not found in the given string.

Example 1: Count Method on a String

The following example shows the working of count() function on a string. Output:

Example 2: Count occurrence of a character in a given string

The following example shows the occurrence of a character in a given string as well as in by using the start/end index. Output:

Example 3: Count occurrence of substring in a given string

Following example shows the occurrence of substring in a givenstring as well as usingstart/endindex. Output:

Summary:

The count() is a built-in function in Python. It will return you the count of a given element in a list or a string. In the case of a string, the counting begins from the start of the string till the end. It is also possible to specify the start and end index from where you want the search to begin. The count() method returns an integer value.