Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

Python count

by suresh

Python count method is useful to count the total number of times a substring repeated in a given string. In this section, we discuss how to use the count function to perform counting in Python Programming with example.

Python count Function Syntax

The syntax of the count function in Python Programming Language is

String_Value.count(Sub, Start, End)
  • String_Value: Please select a valid String variable, or use the String directly.
  • Sub: This argument is required. Count method looks for this substring inside the String_Value, and if it finds, then it returns the count value.
  • Start: You can specify the starting value here. For example, if you want to count from the middle of a string or at a particular index, you can specify here.
  • End: You can specify the end value here. For example, if you want to count up to the middle of a string, or up to a particular index, you can specify here.

Return Value

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

Python count method Example

The following set of examples help to understand the string count Function in Python Programming Language.

# Python Count Method Example
 
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 Count function directly
Str6 = 'learn Python at tutorial gateway'.count('a', 0, 60)
print("Total Number of a's in String1 = ", Str6)

OUTPUT

Python Count Example 1

ANALYSIS

In this Python example, First, we declared three String variable Str1, Str2, and assigned corresponding value using the following statement.

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

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

Str3 = Str1.count(Str2)
print("Total Number of a's in String1 = ", Str3)

The following python count statement slice the string Str1 starting at 6 and up to 25. Within the sliced string, python counts the number of times ‘a’ string repeated and prints the output.

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

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, Python count the number of times ‘a’ repeated.

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

In the below statement, we used the count function directly on String.

Str6 = 'learn Python at tutorial gateway'.count('a', 0, 60)
print("Total Number of a's in String1 = ", Str6)

Python count function Alternative 2

In the interview perspective, they may ask you to display the letter count without using the built-in count method. In this example, we use for loop to iterate the characters present in String.

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

count = 0

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

OUTPUT

Python Count Example 2

ANALYSIS

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

for char 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 Count value incremented by 1.

 if char == Str2:
     count = count + 1

The last print statement prints the Output.

print("Total Number of a's in String1 = ", count)

Python count function Alternative 2

The following example is another alternative method to the String count function in Python Programming Language.

from collections import Counter

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

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

OUTPUT

Python Count Example 3

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python Numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python Numpy concatenate
  • Python Numpy Array shape
  • Python Pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python Substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy