Python variable as a string

Jake Angulo jake.angulo at gmail.com
Sat Aug 24 07:11:55 EDT 2013


Thank you all for the reply.

Actually yes this was a confusing question, and borne out of trying to make
a shortcut.
I didnt ask to convert the contents of var into a string.
All I needed was to get the literal equivalent "var" because I needed to
use it in another dict object - whose keys i named the same (eg 'var') for
convenience.  Instead i ended up complicating stuff.

I resolved this by doing things differently with (if - elif - else).

Sorry for the confusion - but thanks all for answering - i can use a code
or two of what you have shared!



On Fri, Aug 23, 2013 at 11:23 PM, Neil Cerutti <neilc at norwich.edu> wrote:

> On 2013-08-23, Jake Angulo <jake.angulo at gmail.com> wrote:
> > I have a list *var* which after some evaluation I need to refer
> > to *var* as a string.
>
> You must make a str version of var.
>
> > Pseudocode:
> >
> > var = ['a', 'b' , 'c' , 'd']
> > adict = dict(var='string', anothervar='anotherstring')
> > anotherdict = dict()
> > if <condition>:
> >     anotherdict[akey] = adict['var']
>
> anotherdict[akey] = adict[str(var)]
>
> Will actually work, though you might prefer:
>
> anotherdict[akey] = adict[''.join(var)]
>
> Try them out and see.
>
> --
> Neil Cerutti
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130824/fb45d4db/attachment.html>


More information about the Python-list mailing list