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.
❮ String Methods
Example
Return the number of times the value "apple" appears in the string:
txt = "I love apples, apple are my favorite fruit"
x = txt.count["apple"]
print[x]
Try it Yourself »
Definition and Usage
The count[]
method returns the number of times a specified value appears in the string.
Syntax
string.count[value, start, end]
Parameter Values
value | Required. A String. The string to value to search for |
start | Optional. An Integer. The position to start the search. Default is 0 |
end | Optional. An Integer. The position to end the search. Default is the end of the string |
More Examples
Example
Search from position 10 to 24:
txt = "I love apples, apple are my favorite fruit"
x = txt.count["apple", 10, 24]
print[x]
Try it Yourself »
❮ String Methods
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Python String count[] function is an inbuilt function in python programming language that returns the number of occurrences of a substring in the given string.
Syntax:
string.count[substring, start=…, end=…]
Parameters:
- The count[] function has one compulsory and two optional parameters.
- Mandatory parameter:
- substring – string whose count is to be found.
- Optional Parameters:
- start [Optional] – starting index within the string where the search starts.
- end [Optional] – ending index within the string where the search ends.
Return Value:
count[] method returns an integer that denotes number of times a substring occurs in a given string.
Example 1: Implementation of the count[] method without optional parameters
Python3
string
=
"geeks for geeks"
print
[string.count[
"geeks"
]]
Output:
2
Example 2: Implementation of the count[] method using optional parameters
Python3
string
=
"geeks for geeks"
print
[string.count[
"geeks"
,
0
,
5
]]
print
[string.count[
"geeks"
,
0
,
15
]]
Output:
1 2