Iterate over words of a String in Python
Given a String comprising of many words separated by space, write a Python program to iterate over these words of the string.
Examples:
Input: str = “GeeksforGeeks is a computer science portal for Geeks”
Output: GeeksforGeeks is a computer science portal for Geeks
Input: str = “Geeks for Geeks”
Output: Geeks for Geeks
Method 1: Using split() 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.
Python3
test_string = "GeeksforGeeks is a computer science portal for Geeks"
print ( "The original string is : " + test_string)
res = test_string.split()
print ( "\nThe words of string are" )
for i in res:
print (i)
|
Output
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
Time complexity: O(n)
Auxiliary Space: O(n)
Method 2: Using re.findall() 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.
Python3
import re
test_string = "GeeksforGeeks is a computer science portal for Geeks !!!"
print ( "The original string is : " + test_string)
res = re.findall(r '\w+' , test_string)
print ( "\nThe words of string are" )
for i in res:
print (i)
|
Output
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 3: Using for loop and string slicing
Python3
test_string = "GeeksforGeeks is a computer science portal for Geeks"
print ( "The original string is: " + test_string)
start = 0
for i in range ( len (test_string)):
if test_string[i] = = ' ' :
print (test_string[start:i])
start = i + 1
print (test_string[start:])
|
Output
The original string is: GeeksforGeeks is a computer science portal for Geeks
GeeksforGeeks
is
a
computer
science
portal
for
Geeks
This approach uses a for loop to iterate through the characters in the string and a variable to keep track of the starting index of the current word. When a space is encountered, the current word is printed and the starting index is updated to the next character. The last word in the string is printed outside the loop.
Time Complexity: O(n)
Auxiliary Space : O(n)
Method 4: Using the split() method with a regular expression
Use the split() method with a regular expression to split the string into words
Step-by-step approach:
- Initialize a list to store the words extracted from the string.
- Use the split() method with a regular expression to split the string into words.
- Iterate over the words and append them to the list.
- Print the list of words.
Follow the below steps to implement the above idea:
Python3
import re
test_string = "GeeksforGeeks is a computer science portal for Geeks !!!"
print ( "The original string is : " + test_string)
res = re.split(r '\W+' , test_string)
words = []
for word in res:
if word:
words.append(word)
print ( "\nThe words of string are" )
for word in words:
print (word)
|
Output
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
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), where n is the length of the input string.
Last Updated :
30 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...