Skip to content Skip to sidebar Skip to footer

How To Ignore The Escaping \ Python List?

I want to ignore the escape character in the following code. >>> a=['\%'] >>> print a ['\\%'] I want to output like ['\%']. Is there any way to do that?

Solution 1:

Using string_escape, unicode_escape encoding (See Python Specific Encodings):

>>> a = ['\%']
>>> print str(a).decode('string_escape')
['\%']
>>> print str(a).decode('unicode_escape')
['\%']

Solution 2:

Couple of manual ways:

>>> a=['\%']
>>> print "['{}']".format(a[0])
['\%']
>>> print "['%s']" % a[0]
['\%']

Or more generally:

>>> a=['\%', '\+']
>>> print '[{}]'.format(', '.join("'{}'".format(i) for i in a))
['\%', '\+']
>>> print '[%s]' % ', '.join("'%s'" % i for i in a)
['\%', '\+']

Post a Comment for "How To Ignore The Escaping \ Python List?"