Given a String comprising of many words separated by space, write a Python program to iterate over these words of the string.
Input: str = “GeeksforGeeks is a computer science portal for Geeks”
Input: str = “Geeks for Geeks”
Method 1: Using
split() function, we can split the string into a list of words and is the most generic and recommended method if one wished to accomplish this particular task. But the drawback is that it fails in the cases in string contains punctuation marks.
The original string is : GeeksforGeeks is a computer science portal for Geeks The words of string are GeeksforGeeks is a computer science portal for Geeks
Method 2: Using
In the cases which contain all the special characters and punctuation marks, as discussed above, the conventional method of finding words in a string using split can fail and hence requires regular expressions to perform this task.
findall() function returns the list after filtering the string and extracting words ignoring punctuation marks.
The original string is : GeeksforGeeks is a computer science portal for Geeks!!! The words of string are GeeksforGeeks is a computer science portal for Geeks
- Iterate over characters of a string in Python
- Python | Extract words from given string
- Python | Words lengths in String
- Reverse words in a given String in Python
- Python | Extract odd length words in String
- Python - Words Frequency in String Shorthands
- Python program to print even length words in a string
- Python - Eliminate Capital Letter Starting words from String
- Iterate over a set in Python
- Iterate over a dictionary in Python
- Iterate over a list in Python
- Python | Iterate through value lists dictionary
- Python - Iterate over Columns in NumPy
- Python Iterate over multiple lists simultaneously
- Python | Ways to iterate tuple list of lists
- Loop or Iterate over all or certain columns of a dataframe in Python-Pandas
- Count words in a given string
- Minimum Distance Between Words of a String
- Possible Words using given characters in Python
- Python | Stemming words with NLTK
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.