string to list conversion

Steve Holden steve at holdenweb.com
Thu Feb 19 08:07:51 EST 2009


John Forse wrote:
> I need to convert an input string say 'xxxx' to a list of the form
> ['xxxx' ,]. If I use list(stringname), I get ['x','x','x','x'] ;
> list.join() is an error;  and str.join() won't use lists. I do need the
> comma after the string. Is there a simple solution?

Suppose your input string is s. Just say

s = [s]

Bingo, s is now a list containing the input string as its only element.
But I suspect that's not what you mean ... because you say "I do need
the comma after the string". Do you mean that you want to produce a
string containing "['xxxx', ]"?

You might try

s = "['%s', ]" % s

But that's potentially going to give you problems if s contains either
an apostrophe or a quote mark. It depends how you plan to use it. So
what is it you want, exactly, and (if it's not asking too much) why?

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list