Table of Contents
In this tutorial, we will learn about the Python String count() method with the help of examples.
The count() method returns the number of occurrences of a substring in the given string.
Example
message = 'python is popular programming language'
# number of occurrence of 'p'
print('Number of occurrence of p:', message.count('p'))
# Output: Number of occurrence of p: 4
1. Syntax of String count
The syntax of count() method is:
string.count(substring, start=..., end=...)
2. count() Parameters
count() method only requires a single parameter for execution. However, it also has two optional parameters:
- substring – string whose count is to be found.
- start (Optional) – starting index within the string where search starts.
- end (Optional) – ending index within the string where search ends.
Note: Index in Python starts from 0, not 1.
3. count() Return Value
count() method returns the number of occurrences of the substring in the given string.
4. Example 1: Count number of occurrences of a given substring
# define string
string = "Python is awesome, isn't it?"
substring = "is"
count = string.count(substring)
# print count
print("The count is:", count)
Output
The count is: 2
5. Example 2: Count number of occurrences of a given substring using start and end
# define string
string = "Python is awesome, isn't it?"
substring = "i"
# count after first 'i' and before the last 'i'
count = string.count(substring, 8, 25)
# print count
print("The count is:", count)
Output
The count is: 1
Here, the counting starts after the first i has been encountered, i.e. 7th index position.
And, it ends before the last i, i.e. 25th index position.
Related posts:
Python Program to Randomly Select an Element From the List
Count Occurrences of a Char in a String
Python Program to Represent enum
Java InputStream to String
Python max()
Python Program to Check If a List is Empty
Python String zfill()
Python String format_map()
Python Get Current time
Python Program to Print Hello world!
Python String lower()
Python Program to Check if a Number is Odd or Even
Python String capitalize()
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python String isspace()
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Node.js vs Python for Backend Development
Python Program to Find All File with .txt Extension Present Inside a Directory
Converting a Stack Trace to a String in Java
Python Program to Find Numbers Divisible by Another Number
Python pass statement
Python Dictionary
Python Program to Convert Kilometers to Miles
Python Program to Count the Occurrence of an Item in a List
Format ZonedDateTime to String
Python Program to Find LCM
Python Program to Make a Simple Calculator
Python String index()
Python Program to Find ASCII Value of Character
Python Program to Find Sum of Natural Numbers Using Recursion
Python String endswith()
Python Program to Remove Punctuations From a String