| | |
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 23 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; 23 Days Ago at 3:34 pm.
No one died when Clinton lied.
•
•
Join Date: Nov 2007
Posts: 138
Reputation:
Solved Threads: 27
0
#3 23 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 23 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; 23 Days Ago at 6:48 pm.
To P(ython), or not to P(ython)
•
•
Join Date: Nov 2007
Posts: 138
Reputation:
Solved Threads: 27
1
#5 23 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: 76
Reputation:
Solved Threads: 17
Python Syntax (Toggle Plain Text)
print ' '.join(list('Hello World'))
0
#8 21 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; 21 Days Ago at 10:07 am.
May 'the Google' be with you!
•
•
Join Date: Nov 2009
Posts: 76
Reputation:
Solved Threads: 17
... 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; 15 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 |
abrupt ansi anti apache approximation array assignment avogadro backend beginner binary bluetooth book builtin calculator character cmd converter countpasswordentry curved customdialog dan08 decimals dictionaries dictionary dynamic error exe file float format function gnu graphics heads homework http ideas import inches input java leftmouse library line lines linux list lists loop module mouse mysqlquery number numbers numeric output parsing path phonebook plugin pointer prime programming progressbar py2exe pygame python random recursion redirect scrolledtext software sqlite statictext statistics string strings sum table terminal text textarea thread threading time tlapse trick tuple tutorial twoup ubuntu unicode urllib urllib2 variable wordgame write wxpython xlib






