The \x
in y
is not an actual backslash character followed by an x
character, but rather the start of an escape sequence.
The whole sequence "\x53"
is "S"
(because "S"
is 0x53
in ASCII). Similarly, "\x69"
is "i"
and "\x6d"
is "m"
.
When you do your replace
call to compute f
, you don't get the escape sequence, but rather an actual backslash character followed by an x
character.
I'm not sure there is a completely straight forward way to go from a hex string to a regular one, but this regex substitution will do the trick with some help from int
and chr
:
re.sub("#(..)", lambda match: chr(int(match.group(1), 16)), s)