
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Find the first repeated word in a string in Python?
One string is given .Our task is to find first repeated word in the given string.To implement this problem we are using Python Collections. From the collection, we can get Counter() method.
Algorithm
Repeatedword(n) /* n is the string */ Step 1: first split given string separated by space into words. Step 2: now convert the list of words into a dictionary. Step 3: traverse list of words and check which the first word has frequency >1
Example code
# To Find the first repeated word in a string from collections import Counter def repeatedword(n): # first split given string separated by space into words w = n.split(' ') con = Counter(w) for key in w: if con[key]>1: print ("REPEATED WORD IS ::>",key) return # Driver program if __name__ == "__main__": n=input("Enter the String ::") repeatedword(n)
Output
Enter the String ::We are all peaceful soul and blissful soul and loveful soul happy soul REPEATED WORD IS ::> soul
- Related Articles
- Find the first repeated word in a string in Python using Dictionary
- Find the first repeated word in a string in Java
- Find the first repeated word in a string in C++
- Find the first repeated character in a string using C++.
- Find repeated character present first in a string in C++
- How to Find the Most Repeated Word in a Text File using Python?
- Second most repeated word in a sequence in Python?
- Find the second most repeated word in a sequence in Java
- Find the first maximum length even word from a string in C++
- Python - Find the length of the last word in a string
- Find First and Last Word of a File Containing String in Java
- Find frequency of each word in a string in Python
- How to replace only the first repeated value in a string in MySQL
- C++ program to find Second most repeated word in a sequence
- Python – Word Frequency in a String

Advertisements