[Tutor] Converting a list to a string

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Mon, 7 May 2001 08:33:16 -0700 (PDT)


On Mon, 7 May 2001, Praveen Pathiyil wrote:

> If i have a list 
> status = ['tftp>', 'Sent', '1943', 'bytes', 'in', '0.0', 'seconds'],
> is there a single command which will give me a string 
> tftp> Sent 1943 bytes in 0.0 seconds
> 
> OR do i have to do 
> 
> stat_str = ''
> for elt in status:
>     stat_str = stat_str + ' ' + elt

People have suggested using string.join(), which is how I'd approach this
problem.

However, if you already know how many elements are in your status list,
you can also use string interpolation toward the cause.

    stat_str = "%s %s %s %s %s %s %s %s" % tuple(status)

would work, assuming that status is an 8-element list.

Good luck!