Python count

Python count function is useful to count the total number of times a substring is repeated in a given string. This section discusses how to use the count function to perform counting with an example.

count Syntax

The syntax of the count function in Python Programming Language is

String_Value.count(Sub, Start, End)
  • Sub: This argument is required. The count Function looks for this substring inside the String_Value, and if it finds the text, then it returns the result.
  • Start: If you want to count from the middle of a string or at a particular index, you can specify it here.
  • End: If you want to find up to the middle of a string or up to a particular index, you can specify it here.

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

Python count Function Example

The following set of examples helps to understand the string count Function in this Programming Language.

In this Python example, First, we declared two String variable Str1, Str2, and assigned corresponding values.

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

The following count statement slice the string Str1 starting at 6 and up to 25. Within the sliced string, it finds the number of times ‘a’ string repeated 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 python len function). Within the sliced string, count the number of times ‘a’ repeated.

Within the last statement Str6, we used the 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)
Count Example 1

Python count function Alternative 2

In 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.

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 If statement to check whether each of the string characters is equal to Str2 or not. And if it is True, then co value incremented by 1.

The last print statement prints the Output.

The following example is another alternative method to this 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