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 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz