RSS Forums RSS
Please support our Python advertiser: Programming Forums
Views: 1960 | Replies: 4
Reply
Join Date: Jun 2005
Posts: 23
Reputation: Rete is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Rete Rete is offline Offline
Newbie Poster

Question about loops and strings

  #1  
Jun 29th, 2005
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!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2004
Posts: 2,536
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Solution Re: Question about loops and strings

  #2  
Jun 30th, 2005
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]
May 'the Google' be with you!
Reply With Quote  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: Question about loops and strings

  #3  
Jun 30th, 2005
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.
Reply With Quote  
Join Date: Oct 2004
Posts: 2,536
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Solution Re: Question about loops and strings

  #4  
Jun 30th, 2005
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!
May 'the Google' be with you!
Reply With Quote  
Join Date: Jun 2005
Posts: 23
Reputation: Rete is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Rete Rete is offline Offline
Newbie Poster

Re: Question about loops and strings

  #5  
Jun 30th, 2005
Ahhhh thanks very much :mrgreen:
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 11:25 am.
Newsletter Archive - Sitemap - Privacy Statement - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC