Iterate over the words of a string in Python

In this post, we will iterate over the words of string in Python. Iteration means going through each element one by one. Thanks to Python’s versatility we have many methods in Python to do the same. We are going to input a string and the output will separate all the words into different lines.

Input: This is a string of words.

 Output: This

 is

 a

 string

 of

 words

Method 1: using split()

The most general and suggested technique to complete this specific task is to divide the string into a list of terms using the split() function.

Code for split() function:

# Python3 code to demonstrate
# to extract words from string
# using split()
  
# input the string
initial_string = "This is a string of words"
  
# printing original string
print ("The initial string : " + initial_string)
  
# using split()
# to extract words from string
result = initial_string.split()
  
# printing result
print ("\nThe words are")
for _ in result:
  print(_)

Output:

The initial string: This is a string of words

The words are
This
is
a
string
of
words.

Method 2: Using re.findall()

When a string contains all special characters and parentheses, the traditional approach of detecting words in a string using split may not work, necessitating the use of logical operators. After filtering the string and extracting the words while avoiding punctuation, the findall() method produces the list.

# Python3 code to demonstrate
# to extract words from string
# using regex( findall() )
import re
  
# initializing string
initial_string = "This is a test string of words ! !"
  
# printing original string
print ("The initial string is : " + initial_string)
  
# using regex( findall() )
# to extract words from string
result = re.findall(r'\w+', initial_string)
  
# printing result
print ("\nThe words of string are")
for _ in result:
  print(_)

Output:

The initial string is : This is a test string of words ! !

The words of string are
This
is
a
test
string
of
words

The only difference between the two methods is that the split() method is not suitable when a string contains punctuation. Using re.finadall is considered more efficient.

Learn, to split an array into n parts of almost equal length in Python

Leave a Reply

Your email address will not be published. Required fields are marked *