You are currently viewing Python String count() Method

How to count a string in Python? You can use the built-in count() function of Python which is used to count the number of occurrences of a substring within a given string. It returns an integer that represents the count of a specified substring which, can appear the number of times within a string. This string method is commonly used for tasks such as finding how many times a particular word or sequence of characters appears in a larger text.

Advertisements

In this article, I will explain the Python string count() method and using its syntax, parameters, and usage how we can count the number of occurrences of a substring within a given string with examples.

Related: In Python, you can count all characters present in a string.

1. Quick Examples of String count() Method

If you are in a hurry, below are some quick examples of the string count() method.


# Quick examples of string count() method

# Initialize the string
mystring = "Welcome to sparkbyexamples"

# Example 1: Use string count() method
count = mystring.count("e")

# Example 2: Use String Count() function
# Count occurrences of a given substring
count = string.count('Welcome')

# Example 3: Counting the occurrences of a substring 
# with start and end Parameters
count = string.count('m', 4, 8)

# Example 4: Counting the occurrences of a substring 
# Using re.findall()
substring = "Welcome"
count = len(re.findall(substring, string))

# Example 5: Count occurrences of acharacter in multiple strings 
array of strings = ["Python", "Java", "Spark","Pandas"]
character_to_count = "a"
count = sum(string.count(character_to_count) for string in strings)

2. Python String count() Method

The Python string count() method is used to count the number of non-overlapping occurrences of a substring within a given string. It can return the count occurrences of a substring within a specified interval.

2.1 Syntax of count()

Following is the syntax of the string count() method.


# Syntax of count() method
string.count(substring, start, end)

2.1 Parameter of count()

  • substring – The substring you want to count within the string.
  • strart (optional) – The starting index from where the search should begin. The default value is 0 (the beginning of the string).
  • end (optional) – The index at which the search should end (default is the end of the string).

2.2 Returns Value

It returns the count of occurrences of the specified value(substring) in the string. It doesn’t return a value other than the count itself.

3. Usage of Python String Count

You can use the count() method to count the number of occurrences of a specific character. In this case, the character e within the given string.

In the below example, first, you can initialize a string mystring with the value "Welcome to sparkbyexamples". You can apply the count() method over the given string to count the number of times the character “e” appears in the string. For example,


# Initialize the string
mystring = "Welcome to sparkbyexamples"
print("Original string: ", mystring)

# Use string count() method to count the 
# occurences of specifief substring
count = mystring.count("e")
print('Count occurrences of the specified substring:', count)

Yields below output.

python string count

4. Count the Occurrences of Python Substring

You can Pass the specified substring of a given string into this function it will return a total number of occurrences of the substring within a string. In this example, you can pass the substring "Welcome" into this function will return the count of its occurrences within a string.


# Initialize the string
string = "Welcome to sparkbyexamples"
print("Original string:",string)

# Use String Count() function
# Count occurrences of a given substring
count = string.count('Welcome')
print("Count occurrences of the specified substring:", count)

Yields below output.

python string count

5. Count the Occurrences of a Substring with Start and End Parameter

Similarly, you can use the str.count() method to count the occurrences of a substring within a specified interval. In the below example, the original string is "Welcome to sparkbyexamples" and passes the specified character 'm' into this function along with the start(4 inclusive), and the end(7 exclusive) parameters.

Finally, it counts the occurrences of ‘m’ within the substring “come”(positions 4 to 7) in the original string. There is one occurrence of ‘m’ in that substring, hence the count is 1.


# Initialize the string
string = "Welcome to sparkbyexamples"
print("Original string:",string)

# Counting the occurrences of a substring 
# with start and end Parameters
count = string.count('m', 4, 8)
print("Count occurrences of the specified substring:", count)

# Output:
# Original string: Welcome to sparkbyexamples
# Count occurrences of the specified substring: 1

6. Using Regular Expression

You can also use the re.findall() function from the re module to count the occurrences of a substring in the original string. For instance, define the original string "Welcome to sparkbyexamples" and substring "Welcome" and pass them into this function, it will return all occurrences of a passed substring within a string with the help of len() function.


import re

# Initialize the string
string = "Welcome to sparkbyexamples"
print("Original string:",string)

# Counting the occurrences of a substring 
# Using re.findall()
substring = "Welcome"
count = len(re.findall(substring, string))
print("Count occurrences of the specified substring:", count)

# Output:
# Original string: Welcome to sparkbyexamples
# Count occurrences of the specified substring: 1

7. Count Occurrences of Character in Multiple Strings

You can also count the occurrences of a character in multiple strings using the count() function. For instance, you can create an array of strings. Then use a generator expression inside the sum() function to count the occurrences of ‘a’ in each string within the strings using for loop. The string.count(character_to_count) expression calculates the count of ‘a’ in each string and adds these individual counts using the sum() function.


# Initialize the array of strings
strings = ["Python", "Java", "Spark","Pandas"]
print("Original string:",strings)

# Count occurrences of character in multiple string 
character_to_count = "a"
count = sum(string.count(character_to_count) for string in strings)
print("Count occurrences of the specified substring:", count)

# Output:
# Original string: ['Python', 'Java', 'Spark', 'Pandas']
# Count occurrences of the specified substring: 5

Conclusion

In this article, I have explained the Python string count() method and using its syntax, parameters, and usage of how we can count the number of occurrences of a specific character/substring within a given string with examples.

Happy Learning !!