Programming Examples

Python to print the number of occurrences of a sub string into a line


WAP to print the number of occurrences of a substring into a line.

Solution

#WAP to print the number of occurrences of a substring into a line.
s=input("Enter a String : ")
substr=input("Enter a sub String : ")
l=len(s)
lsub=len(substr)
start=count=0
end=l
while True:
    position=s.find(substr,start,end)
    if position!=-1:
        count+=1
        start=position+lsub
    else:
        break
    if start>=l:
        break;
print("No. of Occurrence of : ",substr," : ",count)
Output

Enter a String : infomax computer academy

Enter a sub String : max

No. of Occurrence of :  max  :  1

__________________________________________________

Enter a String : printer print the paper

Enter a sub String : print

No. of Occurrence of :  print  :  2


CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online Exam Quiz O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Best Website and Software Company in Allahabad Website development Company in Allahabad