[Tutor] Why does print add spaces ?

Bob Gailer ramrom@earthling.net
Sat Mar 15 19:32:01 2003


--=======5E1E58D=======
Content-Type: text/plain; x-avg-checked=avg-ok-900D57; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 8bit


> > In this example
> >
> >
> > mystr="abcdefghi"
> >
> > for x in range(1,len(mystr)):
> >     print "%s," % mystr[x],
> >
> >
> > a, b, c, d, e, f, g, h, i,
> >
> > why does print add an implied space ??

An (obvious) alternative is to construct the desired output as one string, 
then print it. If the desired output is
a,b,c,d,e,f,g,h,i, you could:

out = ''
for x in range(1,len(mystr)):
      out += "%s," % mystr[x]
print out

Since mystr is a sequence this simplifies to:

out = ''
for x in mystr:
      out += "%s," % x
print out

Even simpler:

print ','.join(mystr) + ','


Bob Gailer
mailto:ramrom@earthling.net
303 442 2625

--=======5E1E58D=======
Content-Type: text/plain; charset=us-ascii; x-avg=cert; x-avg-checked=avg-ok-900D57
Content-Disposition: inline


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.459 / Virus Database: 258 - Release Date: 2/25/2003

--=======5E1E58D=======--