Q. Write a program to print the number of occurrences of a sub-string into a line using built-in string function find().
Answer =
sen = input("Enter a sentence :- ") sub = input("Enter a sub-string :- ") count = 0 n = 0 while n < len(sen) : n = sen.find(sub,n) if n == -1 : break else : count += 1 n += 1 print("Number of ", sub, "is :- ", count)
Output :-
Enter a sentence :- the path walla is the website.
Enter a sub-string :- the
Number of the is :- 2
>>>
Enter a sentence :- hello world, hello friends, hello hello !
Enter a sub-string :- hello
Number of hello is :- 4
>>>
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )