Q. Write a Python program to find the second most repeated word in a given string.
Answer =
string = input("Enter a string :-") lst = string.split() max = 0 sec = 0 for i in lst: if lst.count(i) >= max : max = lst.count(i) elif lst.count(i) >= sec : sec = lst.count(i) secmaxvalue = i print("Second most repeated word :-", secmaxvalue )
Output :-
Enter a string :-This is Pathwalla website This provide you solution of sumita arora class 11 , 12 This is our pleasure.
Second most repeated word :- is
>>>
Here "This" repeated 3 times and "is" repeated 2 times.
actually the question has asked for the second most repeated word but the above code is for the most repeated word.
ReplyDeleteSorry, Please read again I have corrected it.
Deletewrong answer
ReplyDeleteit will display the most repeated word in sentence not the second most repeated word
Sorry, Please read again I have corrected it.
DeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )