Write a Python program to find the first repeated word in a given string


This program is finding the duplicate words in a string "Hello world ! Hello Tutor Joes". The program first initializes a set t to store unique words. The program then uses a for loop to iterate through each word in the string, which is obtained by splitting the string using the split() method. For each word, the program checks if the word is already in the set t. If the word is already in the set t, it means that the word is a duplicate, and it is printed to the console using the print function. If the word is not in the set t, it is added to the set using the add method.

Source Code

str = "Hello world ! Hello Tutor Joes"
t = set()
for txt in str.split():
	if txt in t:
	  print(txt)
	else:
	  t.add(txt)

Output

Hello


Example Programs