Please support our Python advertiser: Programming Forums
Views: 1960 | Replies: 4
![]() |
•
•
Join Date: Jun 2005
Posts: 23
Reputation:
Rep Power: 4
Solved Threads: 0
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
H
e
l
l
o
etc.
I was wondering if I could make it so that it would instead print out like
Hello
World
Any help would be greatly appreciated!
for letter in "Hello world"
print letter
it would print out
H
e
l
l
o
etc.
I was wondering if I could make it so that it would instead print out like
Hello
World
Any help would be greatly appreciated!
You can split the string into a list of words at a whitespace (space, spaces, tab, newline) ...
[php]# 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
print
# now print out each word in the list on a separate line
for word in wordList:
print word
[/php]
[php]# 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
[/php]
May 'the Google' be with you!
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode