Friday, December 27, 2024
Google search engine
HomeLanguagesPython String count() Method

Python String count() Method

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




# Python program to demonstrate the use of
# count() method without optional parameters
 
# string in which occurrence will be checked
string = "Lazyroar for Lazyroar"
 
# counts the number of times substring occurs in
# the given string and returns an integer
print(string.count("Lazyroar"))


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




# Python program to demonstrate the use of
# count() method without optional parameters
 
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




# Python program to demonstrate the use of
# count() method  using optional parameters
 
# string in which occurrence will be checked
string = "Lazyroar for Lazyroar"
 
# counts the number of times substring occurs in
# the given string between index 0 and 5 and returns
# an integer
print(string.count("Lazyroar", 0, 5))
 
print(string.count("Lazyroar", 0, 15))


Output

1
2

Time Complexity: O(n)
Auxiliary Space: O(1)

RELATED ARTICLES

Most Popular

Recent Comments