3

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?

1
  • 3
    Are you sure you understand what you're seeing here? In python, '\\%' == '\%' - both represent a backslash character followed by a percent character. Commented Dec 18, 2013 at 12:37

2 Answers 2

8

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

>>> a = ['\%']
>>> print str(a).decode('string_escape')
['\%']
>>> print str(a).decode('unicode_escape')
['\%']
Sign up to request clarification or add additional context in comments.

Comments

0

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)
['\%', '\+']

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.