I was wondering if there was any way to make a for loop go through a string word by word, rather then character by character. For example,

for letter in "Hello world"
print letter

it would print out


I was wondering if I could make it so that it would instead print out like


Any help would be greatly appreciated!

You can split the string into a list of words at a whitespace (space, spaces, tab, newline) ...

# convert a string to a list of words

str1 = 'You never find a missing item until you replace it'
print "Original string:"
print str1
# split the string into a list of words at a whitespace
wordList = str1.split(None)
print "String split into a list of words:"
print wordList
# now print out each word in the list on a separate line
for word in wordList:
    print word

I was trying to figure out a way to do it manually(without the split method). I feel like I am always trying to reinvent the wheel.

A good part of the fun of programming in Python is the ease with which you can experiment with the code. So go ahead and reinvent a better wheel!

This question has already been answered. Start a new discussion instead.