The count[] is a built-in function in Python. It will return the total count of a given element in 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. In this Python tutorial, you will learn: Python count function syntax: 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. The following example shows the working of count[] function on a string. Output: The following example shows the occurrence of a character in a given string as well as in by using the start/end index.The syntax for PythonString Count[]
string.count[char or substring, start, end]
Parameters of Python Syntax
ReturnValue
Example 1: Count
Method on a String
str1 = "Hello World"
str_count1 = str1.count['o'] # counting the character “o” in the givenstring
print["The count of 'o' is", str_count1]
str_count2 = str1.count['o', 0,5]
print["The count of 'o' usingstart/end is", str_count2]
The count of 'o' is 2
The count of 'o' usingstart/end is 1
Example 2: Count occurrence of a character in a given string
str1 = "Welcome to Guru99 Tutorials!" str_count1 = str1.count['u'] # counting the character “u” in the given string print["The count of 'u' is", str_count1] str_count2 = str1.count['u', 6,15] print["The count of 'u' usingstart/end is", str_count2]
Output:
The count of 'u' is 3 The count of 'u' usingstart/end is 2
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.
str1 = "Welcome to Guru99 - Free Training Tutorials and Videos for IT Courses" str_count1 = str1.count['to'] # counting the substring “to” in the givenstring print["The count of 'to' is", str_count1] str_count2 = str1.count['to', 6,15] print["The count of 'to' usingstart/end is", str_count2]
Output:
The count of 'to' is 2 The count of 'to' usingstart/end is 1
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.
In this tutorial, we will learn about the Python String count[] method with the help of examples.
The count[]
method returns the number of occurrences of a substring in the given string.
Example
message = 'python is popular programming language'
# number of occurrence of 'p'
print['Number of occurrence of p:', message.count['p']]
# Output: Number of occurrence of p: 4
Syntax of String count
The syntax of count[]
method is:
string.count[substring, start=..., end=...]
count[] Parameters
count[]
method only requires a single
parameter for execution. However, it also has two optional parameters:
- substring - string whose count is to be found.
- start [Optional] - starting index within the string where search starts.
- end [Optional] - ending index within the string where search ends.
Note: Index in Python starts from 0, not 1.
count[] Return Value
count[]
method returns the number of occurrences of
the substring in the given string.
Example 1: Count number of occurrences of a given substring
# define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count[substring]
# print count
print["The count is:", count]
Output
The count is: 2
Example 2: Count number of occurrences of a given substring using start and end
# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count[substring, 8, 25]
# print count
print["The count is:", count]
Output
The count is: 1
Here, the counting starts after the first i
has been encountered, i.e. 7th
index position.
And, it ends before the last i
, i.e. 25th
index position.