How to turn a variable name into a string?

Scott David Daniels Scott.Daniels at Acm.Org
Fri Mar 11 15:08:53 EST 2005


Stewart Midwinter wrote:
> I'd like to do something like the following:
> 
> a = 1; b = 2; c = None
> mylist = [a, b, c]
> for my in mylist:
>     if my is None:
>     print 'you have a problem with %s' % my         #this line is problematic
>>>>You have a problem with None
> What I want to see in the output is:
> How do I convert a variable name into a string?

I think you actually want the opposite:

     a = 1; b = 2; c = None
     mylist = ['a', 'b', 'c']
     for my in mylist:
         if globals()[my] is None:
             print 'you have a problem with %s' % my



More information about the Python-list mailing list