[Tutor] converting list to string

Rick Pasotto rick@niof.net
Thu, 22 Mar 2001 23:07:26 -0500


On Thu, Mar 22, 2001 at 10:52:51PM -0500, Benoit Dupire wrote:
> 
> Scott wrote:
> 
> > Please forgive this question, but, I thought this:
> >
> >         x = ['h', 'e', 'l', 'l', 'o']
> >         print str(x)
> >
> > would print:
> >
> >         hello
> >
> > instead I get
> >
> >         ['h', 'e', 'l', 'l', 'o']
> >
> > What am I doing wrong?  I realize it's a silly question, but these
> > immutable strings just aren't being kind to me.  :-(
> >
> > What's the correct way to get a string from a list?  Thanks.
> >
> 
> like this
> >>> x = ['h', 'e', 'l', 'l', 'o']
> >>>import operator
> >>>a=reduce(operator.add, x)
> >>> a
> 'hello'
> >>>

Gosh! Just like that other language "there's more than one way to do it."

>>> x=['h','e','l','l','o']
>>> import string
>>> a=string.join(x,'')
>>> a
'hello'
>>>


-- 
"FIJA is not a double-edged sword --- it is a shield against the sword of
government."
        --- Tom Glass
		   Rick Pasotto email: rickp@telocity.com