Python 3: How do I get a string literal representation of a byte string?
问题 In Python 3, how do I interpolate a byte string into a regular string and get the same behavior as Python 2 (i.e.: get just the escape codes without the b prefix or double backslashes)? e.g.: Python 2.7: >>> x = u'\u041c\u0438\u0440'.encode('utf-8') >>> str(x) '\xd0\x9c\xd0\xb8\xd1\x80' >>> 'x = %s' % x 'x = \xd0\x9c\xd0\xb8\xd1\x80' Python 3.3: >>> x = u'\u041c\u0438\u0440'.encode('utf-8') >>> str(x) "b'\\xd0\\x9c\\xd0\\xb8\\xd1\\x80'" >>> 'x = %s' % x "x = b'\\xd0\\x9c\\xd0\\xb8\\xd1\\x80'"