Tutorial Gateway

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

Python String count

by suresh

Python count function is to count, and return how many times the substring occurred in the specified start and end position. Here start, and end index positions are optional. In this section, we discuss how to write Python String count Function.

Python String count Syntax

The syntax of the Python String Count function is

String_Value.count(Substring, Starting_Position, Ending_Position)
  • String_Value: A valid String literal.
  • Substring: String on which you want to perform Counting.
  • Starting_Position: This is an optional parameter. If you want to specify the starting point (starting index position), then specify the index value here. If you omit this parameter, then Python string count Function consider Zero as a starting position.
  • Ending_Position (Optional): If you want to specify the endpoint (Ending index position), then specify the index value here. If you omit this parameter, the count Function considers the highest index number.

Python String count Example

The following set of examples help you understand the String count Function in Python.

# Python String Count Method Example

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

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

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

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

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

First, we declared the String variable Str1 and assigned string 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 Str1 using count function and prints the output.

Str2 = Str1.count('abc')
print('First Output of a count() method is = ', Str2)

The count function in Python allows us to use the Starting index position. By specifying the starting index position, we can increase String function performance.

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

The count function allows us to use Starting and ending indices. By specifying the starting and ending index positions, we can increase performance. The below Python statement counts the number occurrences from index position 12, and up to the end.

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

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

Str7 = Str1.count('abc', 12, 21)
print('Fifth Output of a count() method is = ', Str7)

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 Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File Handling
  • 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

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy