More Example on String count() Method
Let us see a few examples of the Python String count() method.
Python3
my_string = "w3wiki" char_count = my_string.count( 'e' ) print (char_count) |
4
Below are some examples by which we can count the number of occurence of a substring in python:
- Count Occurrences of a given Substring
- Count Occurrences of Character in Multiple String
- Using Regular Expression
- Using Start and End Parameter
1. Using 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. We can use it to count the number of occurence of a substring in python
Python3
# string in which occurrence will be checked string = "geeks for geeks" # counts the number of times substring occurs in # the given string and returns an integer print (string.count( "geeks" )) |
2
Time Complexity: O(n)
Auxiliary Space: O(1)
2. Using 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) |
4
Time Complexity: O(m*k)
Auxiliary Space: O(k)
3. Using String count() Method with 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) |
2
Time Complexity: O(m)
Auxiliary Space: O(1)
4. Using String count() Method with 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 in which occurrence will be checked string = "geeks for geeks" # counts the number of times substring occurs in # the given string between index 0 and 5 and returns # an integer print (string.count( "geeks" , 0 , 5 )) print (string.count( "geeks" , 0 , 15 )) |
1 2
Time Complexity: O(n)
Auxiliary Space: O(1)
In this article we have covered the definition, syntax and examples of Python string count() method. We have explained the working of string count() function with variations in use cases. String count() method is very important function for string operations.
Read Other Python String Methods
Similar Articles:
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) |
1
In this article, we will explore the details of the count()
method, exploring its syntax, usage, and some practical examples.