Stringify a list

Duncan Booth duncan at NOSPAMrcp.co.uk
Wed May 16 11:58:44 CEST 2001


"Todd A. Jacobs" <nospam at codegnome.org> wrote in
news:mailman.990003323.2806.python-list at python.org: 

> I have a list object that contains card values (i.e. 2-10, J, Q, K, A),
> but I can't seem to print the list in such a way that it doesn't appear
> as follows:
> 
>      [2, 6, 'A', 8, 'J']
> 
> I don't mind the commas, but how do I remove the quotes and brackets
> without going through contortions?
> 

Print the cards individually:
    for card in list: print card,
    print
Or turn each element into a string and join the elements together:
    SEP = ", "
    print SEP.join([str(card) for card in list])
If you prefer, or if you are using Python 1.5 you can also spell that last 
line:
    print string.join(map(str, list), SEP)

If you change your list so that all cards are strings to begin with then 
you can just:
    print SEP.join(list)

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list