simple question

Thread Solved

Join Date: Oct 2009
Posts: 16
Reputation: saikeraku is an unknown quantity at this point 
Solved Threads: 0
saikeraku saikeraku is offline Offline
Newbie Poster

simple question

 
0
  #1
28 Days Ago
I'm just wondering what do I have to add to the code

  1. print "Hello World"

to output

H e l l o W o r l d

like there's spaces in between each letter?

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,273
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven
 
0
  #2
28 Days Ago
You could do:
  1. for c in "Hello World":
  2. print c ,
In Python2 the comma puts in a space and keeps it on the same line.
Last edited by sneekula; 28 Days Ago at 3:34 pm.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 141
Reputation: mn_kthompson is an unknown quantity at this point 
Solved Threads: 32
mn_kthompson mn_kthompson is offline Offline
Junior Poster
 
0
  #3
28 Days Ago
You could also try some string formatting. Check this out...
  1. text = 'Hello World'
  2. for x in text:
  3. print x.rjust(2)
  4.  
  5. # output is H e l l o W o r l d
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 109
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 9
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster
 
1
  #4
28 Days Ago
They're all trying to just confuse you.

  1. print "H e l l o W o r l d"
Last edited by AutoPython; 28 Days Ago at 6:48 pm.
To P(ython), or not to P(ython)
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 141
Reputation: mn_kthompson is an unknown quantity at this point 
Solved Threads: 32
mn_kthompson mn_kthompson is offline Offline
Junior Poster
 
1
  #5
27 Days Ago
Well I assumed that the poster already knew that way to do it. Here's another way to do it. In all of the solutions except AutoPython's the output will contain three spaces in the middle, but the original post only has one space between the o and the w. Here is a way to do that programatically
  1. mystring = 'Hello World'
  2. outstring = ''
  3.  
  4. for x in mystring:
  5. outstring += x
  6. if x != ' ':
  7. outstring += ' '
  8. print outstring
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 79
Reputation: pythopian is an unknown quantity at this point 
Solved Threads: 21
pythopian pythopian is offline Offline
Junior Poster in Training

Jesus Christ guys, pythonic code is SUCCINCT ;-)

 
0
  #6
27 Days Ago
  1. print ' '.join(list('Hello World'))
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 141
Reputation: mn_kthompson is an unknown quantity at this point 
Solved Threads: 32
mn_kthompson mn_kthompson is offline Offline
Junior Poster
 
1
  #7
26 Days Ago
I think Pythopian gets the award for most elegant code. That is just beautiful.
Last edited by mn_kthompson; 26 Days Ago at 12:28 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,009
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 928
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #8
26 Days Ago
Actually, if you temporarily flip to Reply with quote in the OP post you find out the request was for:
  1. H e l l o W o r l d
That is 2 spaces between 'o' and 'W', DaniWeb swallowed up the extra space since it was not in a code field. So the honor goes to mn_kthompson!
Last edited by vegaseat; 26 Days Ago at 10:07 am.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 79
Reputation: pythopian is an unknown quantity at this point 
Solved Threads: 21
pythopian pythopian is offline Offline
Junior Poster in Training

If you MUST have exactly two spaces...

 
0
  #9
19 Days Ago
... between "H e l l o" and "W o r l d":
  1. print ' '.join(c if c != ' ' else '' for c in 'Hello World')
To ensure exactly one space everywhere:
  1. print ' '.join(c for c in 'Hello World' if c != ' ')
Last edited by pythopian; 19 Days Ago at 9:04 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC