String count() Method 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.
- Mandatory parameter:
Return Value: count() method returns an integer that denotes the number of times a substring occurs in a given string.
Example:
Python3
#initializing a string my_string = "Banana" #using string count() method char_count = my_string.count( 'a' ) #printing the result print (char_count) |
Output
3
Python String count() Method
Python String count() function returns the number of occurrences of a substring within a String.
Python3
#initializing a string my_string = "Apple" #using string count() method char_count = my_string.count( 'A' ) #printing the result print (char_count) |
Output
1
In this article, we will explore the details of the count()
method, exploring its syntax, usage, and some practical examples.
Contact Us