Python Programming (136 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (26 Blogs)
  • Data Science (20 Blogs)
  • Mastering Python (86 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

How To Best Utilize Count Function In Python?

Last updated on Nov 27,2024 2K Views


This article will tell you how you can best utilize Count Function in Python and follow it up with a detailed programmatic demonstration Following pointers will be covered in this article,

Table of Content

Let us get started then,

Count Function In Python  

Have you ever wondered how to find the number of occurrences of a particular word in a paragraph or a sentence? Well, if your answer is yes. Then you would not have to look any further. Here is a comprehensive illustration of how to use the popular “Count” function in python programming language. Count is an inbuilt function, which helps the user to determine the number of iterations of a certain sub string within a main string. It could be of two types. The first type is without start end parameters and the second type is with parameters. 

Moving on with this article on Count Function In Python,

Sample Program For Count Function

Let us look at an example to get a better perspective. The below code takes two inputs from the user. One being the main string and the other being the sub string that is to be counted. The output is the number of times “ROSE” has occurred. 

print(“nWelcome to EDUREKA !n”)
string1=input(“Enter the stringn”)
find=input(“nEnter a word to find its occurrencesn”)
print (“The number of occurrences of”, find, “in the sentence  **”, string1, “** is =”)
print(string1.counts(find))
print(“____________________________”)
print(“Thank you have a nice day!”

Output

Output - Count Function In Python - EdurekaMoving on with this article on Count Function In Python,

Count Function Using Optional Parameters 

Optional parameters include providing the length of the main string onto to which the search of occurrence in restricted. For example, when we provide the index or the range from 0 to 10. The search on the main string happens only on the first 11 characters. Hence, the term “ROSE” was found only once in that interval. In case, the length of the main string is increased to cover the second “ROSE”. The corresponding output would be two. 

print(“nWelcome to EDUREKA !n”)
string1=input (“Enter the stringn”)
find= input(“nEnter a word to find its occurrencesn ”)
print(“The number of occurrences of”, find, “in the sentence  **”, string1, “** is =”)
print(string1.count(find,0,10))
print (“_______________________”)
print (“n Thank You have a nice day!”)

Output

Output - Count Function In Python - EdurekaThis brings us to the end of this article on Count Function In Python.

To get in-depth knowledge on Python along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access.

Got a question for us? Mention them in the comments section of  article and we will get back to you.

Upcoming Batches For Python Programming Certification Course
Course NameDateDetails
Python Programming Certification Course

Class Starts on 4th January,2025

4th January

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How To Best Utilize Count Function In Python?

edureka.co