[ACCEPTED]-python compare "\n" escape chars with "\\n-backslash

Accepted answer
Score: 10

How about using unicode_escape encoding?

>>> 'hello\r\n'.encode('unicode_escape') == 'hello\\r\\n'
True
>>> 'hello\r\n' == 'hello\\r\\n'.decode('unicode_escape')
True

In Python 3.x, you 1 need to encode/decode the string/bytes:

>>> 'hello\r\n'.encode('unicode_escape').decode() == 'hello\\r\\n'
True
>>> 'hello\r\n' == 'hello\\r\\n'.encode().decode('unicode_escape')
True

More Related questions