[Tutor] elegant way to turn list into string?

jfouhy@paradise.net.nz jfouhy at paradise.net.nz
Sat May 7 12:31:07 CEST 2005


Quoting Max Russell <max_russell2000 at yahoo.co.uk>:

> anyway- I don't like the output which come in the
> form:
> ['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
> 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
> 'w', 'x', 'y', 'z', 'a']
> 
> 
> What I'd like to do is take my list and turn it into a
> string, which looks neater when printed.
> 
> I've tried using join() but that didn't work, can
> anyone give me a pointer here?

As Tanja points out, you can use ''.join().

You may also be interested in the chr() and ord() built-in functions.

eg:

>>> alphabet = [chr(i) for i in range(ord('a'), ord('z')+1)]
>>> alphabet
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

See also this.py in your lib directory :-)

-- 
John.


More information about the Tutor mailing list