| | |
simple question
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 16
Reputation:
Solved Threads: 0
I'm just wondering what do I have to add to the code
to output
H e l l o W o r l d
like there's spaces in between each letter?
Thanks.
Python Syntax (Toggle Plain Text)
print "Hello World"
to output
H e l l o W o r l d
like there's spaces in between each letter?
Thanks.
0
#2 28 Days Ago
You could do:
In Python2 the comma puts in a space and keeps it on the same line.
python Syntax (Toggle Plain Text)
for c in "Hello World": print c ,
Last edited by sneekula; 28 Days Ago at 3:34 pm.
No one died when Clinton lied.
•
•
Join Date: Nov 2007
Posts: 141
Reputation:
Solved Threads: 32
0
#3 28 Days Ago
You could also try some string formatting. Check this out...
python Syntax (Toggle Plain Text)
text = 'Hello World' for x in text: print x.rjust(2) # output is H e l l o W o r l d
1
#4 28 Days Ago
They're all trying to just confuse you.
Python Syntax (Toggle Plain Text)
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)
•
•
Join Date: Nov 2007
Posts: 141
Reputation:
Solved Threads: 32
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
python Syntax (Toggle Plain Text)
mystring = 'Hello World' outstring = '' for x in mystring: outstring += x if x != ' ': outstring += ' ' print outstring
•
•
Join Date: Nov 2009
Posts: 79
Reputation:
Solved Threads: 21
Python Syntax (Toggle Plain Text)
print ' '.join(list('Hello World'))
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:
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!
Python Syntax (Toggle Plain Text)
H e l l o W o r l d
Last edited by vegaseat; 26 Days Ago at 10:07 am.
May 'the Google' be with you!
•
•
Join Date: Nov 2009
Posts: 79
Reputation:
Solved Threads: 21
... between "H e l l o" and "W o r l d":
To ensure exactly one space everywhere:
Python Syntax (Toggle Plain Text)
print ' '.join(c if c != ' ' else '' for c in 'Hello World')
Python Syntax (Toggle Plain Text)
print ' '.join(c for c in 'Hello World' if c != ' ')
Last edited by pythopian; 19 Days Ago at 9:04 am.
![]() |
Other Threads in the Python Forum
- Previous Thread: Location of site-packages
- Next Thread: Python CGI vs PHP
| Thread Tools | Search this Thread |
address aliased anydbm app bash beginner bits calling casino changecolor cipher clear conversion coordinates corners count cturtle curves definedlines development dictionary digital dynamic events examples excel external feet file float format function gui handling hints homework iframe images import input java keycontrol line linux list lists loan loop matching mouse multiple number numbers output parsing path port prime programming projects py py2exe pygame pymailer python random rational raw_input recursion recursive scrolledtext searchingfile shebang signal singleton split string strings tails terminal text threading time tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 valueerror variable web-scrape whileloop word wxpython xlwt






