🚀 Supercharge your YouTube channel's growth with AI.
Try YTGrowAI FreeHow to Use the Python count() Function

Hi, Folks! In this article, we will be focusing on Python count() method with Strings and Lists.
1. Python count() function with Strings
Python String has got an in-built function – string.count() method to count the occurrence of a character or a substring in the particular input string.
The string.count() method accepts a character or a substring as an argument and returns the number of times the input substring happens to appear in the string.
Syntax:
string.count(string, start_index,end_index)
substring(mandatory): The string whose occurrence of presence needs to be counted in the input string.start_index(optional): The index from where the search for the substring starts.end_index(optional): The index where the search for the substring needs to stop.
Example:
inp_str = "JournalDev -- AskPython @ JournalDev"
str_cnt = inp_str.count("JournalDev")
print(str_cnt)
Output:
2
Example 2:
inp_str = "Python Java Python Kotlin"
str_cnt = inp_str.count("Python", 0 , 6)
print(str_cnt)
In the above example, we have passed ‘Python‘ as a substring to be searched and to count for the presence between index 0 – index 6.
Output:
1
Example 3:
inp_str = "Python Java Python Kotlin"
str_len=len(inp_str)
str_cnt = inp_str.count("Python", 5 , str_len )
print(str_cnt)
Here, we search the substring – ‘Python’ and count its occurrence between index 5 to the end of the string