[Tutor] Joining non strings in to string

Wolfram Kraus kraus at hagen-partner.de
Fri Oct 28 15:01:15 CEST 2005


Eddie S wrote:
> Hi,
> I have a list containing numbers. I want to join this list in to a
> string which I can then output. The problem is I cant seem to join
> list of non-string items in to a string.
> 
> My list looks something like:
> 
> list = [5,7,20,19,49,32]
> 
> I could loop through the list outputing each number individually but I
> am sure there is a nicer method of doing it.
> 
> Cheers Eddie

Use List Comprenhensions, join() and str():

numlist = [5,7,20,19,49,32]
','.join([str(x) for x in numlist])

BTW: never use "list" as a name for your list

http://docs.python.org/tut/node7.html#SECTION007140000000000000000

HTH,
Wolfram



More information about the Tutor mailing list