count() in python by R4R Team

-count() is a python function which is used to count the occurrence of element in the string or list or other data type.
- It take one argument which we want to calculate.

Example-
l=[1,2,3,4,1,1]
l.count(1) will give 3

s="programming"
s.count("p") will give 1
s.count("g") will give 2
s.count("m") will give 2

program-

print("Enter any string")
s=input()
print("Enter substring")
ss=input()
print("Original string is ",s)
print("Orginal substring is ",ss)
ans=s.count(ss,0,len(s))
print("Occurrences of substring in string is ",ans)

l=[1,2,3,4,1,1,1,1,1]
print("list is",l)
print("Occurrences of 1 in list is ",l.count(1))


output-

Enter any string
programming
Enter substring
m
Original string is programming
Orginal substring is m
Occurrences of substring in string is 2
list is [1, 2, 3, 4, 1, 1, 1, 1, 1]
occurrences of 1 in list is 6


Enter any string
this is string of timepass
Enter substring
i
Original string is this is string of timepass
Orginal substring is i
Occurrences of substring in string is 4
list is [1, 2, 3, 4, 1, 1, 1, 1, 1]
Occurrences of 1 in list is 6


-In this program, count() count the element in the list and the string and retuen the occurrence of element.

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!