The \xhh format that you often see is a debugging aid, the repr() output, applied to a string with non-ASCII code points. Any ASCII code points remain in place to leave readable information.
If you must have a line with all characters replaced by \xhh escapes, you need to do this manually:
''.join(r'\x{0:02x}'.format(ord(c)) for c in value)
If you need quotes around this, you also need to add them manually:
"'{0}'".format(''.join(r'\x{:02x}'.format(ord(c)) for c in value))
source share