944,059 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 635
  • Python RSS
Oct 31st, 2009
0

simple question

Expand Post »
I'm just wondering what do I have to add to the code

Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
saikeraku is offline Offline
18 posts
since Oct 2009
Oct 31st, 2009
0
Re: simple question
You could do:
python Syntax (Toggle Plain Text)
  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; Oct 31st, 2009 at 3:34 pm.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Oct 31st, 2009
0
Re: simple question
You could also try some string formatting. Check this out...
python Syntax (Toggle Plain Text)
  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
Reputation Points: 16
Solved Threads: 35
Junior Poster
mn_kthompson is offline Offline
148 posts
since Nov 2007
Oct 31st, 2009
1
Re: simple question
They're all trying to just confuse you.

Python Syntax (Toggle Plain Text)
  1. print "H e l l o W o r l d"
Last edited by AutoPython; Oct 31st, 2009 at 6:48 pm.
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009
Oct 31st, 2009
1
Re: simple question
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
python Syntax (Toggle Plain Text)
  1. mystring = 'Hello World'
  2. outstring = ''
  3.  
  4. for x in mystring:
  5. outstring += x
  6. if x != ' ':
  7. outstring += ' '
  8. print outstring
Reputation Points: 16
Solved Threads: 35
Junior Poster
mn_kthompson is offline Offline
148 posts
since Nov 2007
Nov 1st, 2009
0

Jesus Christ guys, pythonic code is SUCCINCT ;-)

Python Syntax (Toggle Plain Text)
  1. print ' '.join(list('Hello World'))
Reputation Points: 20
Solved Threads: 25
Junior Poster in Training
pythopian is offline Offline
81 posts
since Nov 2009
Nov 2nd, 2009
1
Re: simple question
I think Pythopian gets the award for most elegant code. That is just beautiful.
Last edited by mn_kthompson; Nov 2nd, 2009 at 12:28 am.
Reputation Points: 16
Solved Threads: 35
Junior Poster
mn_kthompson is offline Offline
148 posts
since Nov 2007
Nov 2nd, 2009
0
Re: simple question
Actually, if you temporarily flip to Reply with quote in the OP post you find out the request was for:
Python Syntax (Toggle Plain Text)
  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; Nov 2nd, 2009 at 10:07 am.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 9th, 2009
0

If you MUST have exactly two spaces...

... between "H e l l o" and "W o r l d":
Python Syntax (Toggle Plain Text)
  1. print ' '.join(c if c != ' ' else '' for c in 'Hello World')
To ensure exactly one space everywhere:
Python Syntax (Toggle Plain Text)
  1. print ' '.join(c for c in 'Hello World' if c != ' ')
Last edited by pythopian; Nov 9th, 2009 at 9:04 am.
Reputation Points: 20
Solved Threads: 25
Junior Poster in Training
pythopian is offline Offline
81 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Location of site-packages
Next Thread in Python Forum Timeline: Python CGI vs PHP





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC