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. In this article, we will explore the details of the count() method, exploring its syntax, usage, and some practical examples.
Python String count() Method Syntax
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 the number of times a substring occurs in a given string.
String count() method in Python Examples
Let us see a few examples of the Python String count() method.
Python3
my_string = "GeeksForGeeks"
char_count = my_string.count('e')
print(char_count)
|
Output
4
Python String Count() to Count Occurrences of a given Substring
In this example, Python String Count() will count the occurrences of a given substring and pass only one argument which is mandatory, and skip the optional arguments.
Python3
string = "geeks for geeks"
print(string.count("geeks"))
|
Output
2
Time Complexity: O(n), where n is the length of the string. This is because we need to traverse the entire string to count the occurrence of the substring.
Auxiliary Space: O(1), as we are not using any extra space in the program, only a string variable is used to store the input string.
Python String Count() to Count Occurrences of Character in Multiple String
In this example, Python String Count() will count the occurrences of characters in multiple strings.
Python3
strings = ["Red", "Green", "orange"]
char_to_count = 'e'
total_count = sum(string.count(char_to_count) for string in strings)
print(total_count)
|
Output
4
Time Complexity: O(m*k)
Auxiliary Space: O(k)
Python String count() Method using Regular Expression
In this example, Python String Count() will count the occurrences of single character in the string using Regular Expression.
Python3
import re
my_string = "Good Morning and Morning is Great."
substring = "Morning"
substring_count = len(re.findall(substring, my_string))
print(substring_count)
|
Output
2
Time Complexity: O(m)
Auxiliary Space: O(1)
Python String count() Method using Start and End Parameter
In this example, Python String Count() will count the occurrences of a given string using Start and end parameters and pass all three arguments to find the occurrence of a substring in a Python String.
Python3
string = "geeks for geeks"
print(string.count("geeks", 0, 5))
print(string.count("geeks", 0, 15))
|
Output
1
2
Time Complexity: O(n)
Auxiliary Space: O(1)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
24 Jul, 2023
Like Article
Save Article