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.
What is the String count() Method?
String count() function is an inbuilt function in Python programming language that returns the number of occurrences of a substring in the given string.
It is a very useful string function that can be used for string analysis.
Note: string count() function is case sensitive, meaning it will treat ‘a’ and ‘A’ different.
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) |
3
How to use String count() Method
String count() method is very simple and straightforward to use. You just need to pass the substring as the parameter in function to count it’s number of occurrence. Let’s understand how to count string in Python with an example.
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 = "Beginner for Beginner" # counts the number of times substring occurs in # the given string and returns an integer print (string.count( "Beginner" )) |
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 = "Beginner for Beginner" # counts the number of times substring occurs in # the given string between index 0 and 5 and returns # an integer print (string.count( "Beginner" , 0 , 5 )) print (string.count( "Beginner" , 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:
Contact Us