FAQ: How do I calculate what quoted strings and numbers mean?

Chris Lambacher chris at kateandchris.net
Mon Nov 20 21:54:45 CET 2006


On Mon, Nov 20, 2006 at 11:20:37AM -0800, p.lavarre at ieee.org wrote:
> > but the os module isn't very far away
> 
> I see Python defines some evals that apparently don't import os, such
> as:
> 
> int('0x100'[2:], 0x10)
> float('1.23e+4')
> binascii.unhexlify('4142')
> 
> Does Python also define an eval that doesn't import os for literal
> string args?
> 
> For example, suppose someone gives me source strings like
> repr('\a\r\n') when I need byte strings like '\a\r\n'.
> http://docs.python.org/ref/strings.html lists the escapes that need
> eval'ling, especially the comparatively heavy and Python-specific \N{}
> escape.


You can use string encoding and decoding to do what you want:
>>> a = r'hello\nthere'
>>> print a
hello\nthere
>>> print a.decode('string_escape')
hello
there

you can find other escape type encodings here:
http://docs.python.org/lib/standard-encodings.html

-Chris



More information about the Python-list mailing list