Python String count

This Python String function counts and returns how often the substring occurred in the specified start and end positions. Here start and end index positions are optional. This section discusses how to write the Python String count Function.

This Python count function returns an integer value. For example, If we say, ‘Hell World’.count(‘l’, 0, 7), it starts looking for l inside the ‘Hello World’ and returns two because the index position of the end value is 7 (i., r).

The syntax of the Python String Count function is

String_Value.count(Substring, Starting_Position, Ending_Position)
  • Substring: The string on which you want to use the function, and this argument is mandatory. The count Function looks for this substring inside the String_Value, and if the Python count function finds the text, it returns the result.
  • Starting_Position: This is an optional parameter. If you want to specify the starting point (starting index position), specify it here. For instance, count from the middle of a string or at a particular index. If you omit this parameter, the Python String count function considers Zero as a starting position.
  • Ending_Position (Optional): If you want to specify the endpoint (Ending index position), specify it here. For instance, find up to the middle of a string or a particular index. If you omit this parameter, it considers the highest number.

Python String count Example

Python count function is useful for counting the number of times a substring is repeated in a given string. The following set of examples helps you understand the String count Function.

Str1 = 'We are abc working at abc company with abc Employee';
Str2 = Str1.count('abc')
print('First Output of this method is = ', Str2)

# Performing directly
Str3 = 'Find Tutorial at Tutorial Gateway'.count('Tutorial')
print('Second Output of a method is = ', Str3)

# Using First Index 
Str5 = Str1.count('abc', 12)
print('Third Output of a method is = ', Str5)

# Using First & Second 
Str6 = Str1.count('abc', 12, len(Str1) -1)
print('Fourth Output of a method is = ', Str6)

# Using First & Second
Str7 = Str1.count('abc', 12, 21)
print('Fifth Output of a method is = ', Str7)
Python String Count Function 1

First, we declared the String variable Str1 and assigned data.

Str1 = 'We are abc working at abc company with abc Employee';

The following statement counts the number of times the substring ‘abc’ is repeated inside the string Str1 using this Python function and prints the output.

Str2 = Str1.count('abc')

It also allows us to use the Starting index position. By specifying it, we can increase the String function performance.

Str5 = Str1.count('abc', 12)

It allows us to use Starting and ending indices. By specifying the starting and ending, we can increase performance. The below Python statement count returns the number of occurrences of abc string from index position 12 to the end.

Str6 = Str1.count('abc', 12, len(Str1) -1)

It returns zero because this function starts looking from 12 (which means the first abc skipped) and ends at index position 21. As we all know, the second abc is at 22. It means there are no substrings to tally between those.

Str7 = Str1.count('abc', 12, 21)

count Function Example 2

The following set of examples helps to understand the string count Function in this Python Programming Language. In this Python example, First, we declared two String variables, Str1 and Str2, and assigned corresponding values.

The first statement finds the number of times ‘a’ repeats in the Str1 string and prints the output.

The following Python count statement slices the string Str1 starting at 6 and up to 25. Within the sliced string, it finds the number of times the ‘a’ string will repeat and prints the output.

For Str5, it slices the string Str1 starting at 6 and up to End (To find the end value, we used the len function). Within the sliced string, count the number of times ‘a’ will repeat.

Within the last statement, Str6, we used the function directly on String.

Str1 = 'learn Python at tutorial gateway'
Str2 = 'a'
 
Str3 = Str1.count(Str2)
print("Total Number of a's in String1 = ", Str3)

Str4 = Str1.count(Str2, 6, 25)
print("Total Number of a's in Sliced String1 = ", Str4)

Str5 = Str1.count(Str2, 6, len(Str1))
print("Total Number of a's in Sliced String1 = ", Str5)
 
# Performing directly
Str6 = 'learn Python at tutorial gateway'.count('a', 0, 60)
print("Total Number of a's in String1 = ", Str6)
Python Count function Example 1

Python string count function Alternative

From the interview perspective, they may ask you to display the letter count without using the built-in method. Here, we use for loop to iterate the characters present in String. The if statement compares characters and increments the counter.

Str1 = 'learn Python at tutorial gateway'
Str2 = 'a'

co = 0

for char in Str1:
           if char == Str2:
                      co = co + 1
print("Total Number of a's in String1 = ", co)
Total Number of a's in String1 = 5

In this program, first, We used For Loop to iterate the characters present in Str1.

Within the for loop, we used the If statement to check whether each string character equals Str2. And if it is True, then the cn value is incremented by 1.

The last print statement prints the Output.

The following example is another alternative method to this Python count function.

from collections import Counter

Str1 = 'learn Python at tutorial gateway'
Str2 = 'a'

cn = Counter(Str1)[Str2]
print("Total Number of a's in String1 = ", cn)
Total Number of a's in String1 = 5