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.

4 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. actually the question has asked for the second most repeated word but the above code is for the most repeated word.

    ReplyDelete
    Replies
    1. Sorry, Please read again I have corrected it.

      Delete
  2. wrong answer
    it will display the most repeated word in sentence not the second most repeated word

    ReplyDelete
    Replies
    1. Sorry, Please read again I have corrected it.

      Delete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post