If I have a string containing four characters, for example:
'\\xf0'
How would I convert it to the escape sequence:
'\xf0'
I'm using Python 3.4.
Edit: I was trying to convert the string into the character which the string's value represents.
If I have a string containing four characters, for example:
'\\xf0'
How would I convert it to the escape sequence:
'\xf0'
I'm using Python 3.4.
Edit: I was trying to convert the string into the character which the string's value represents.
What you're trying to do is interpret the escape sequences in the original string, to get the corresponding character(s). Don't compute them yourself, call a decode()
method. In Python 3 you'll only find it on bytes
objects (not str
), so you need to convert to a bytes
object and back:
>>> bytes("\\xf0\\xfa", "utf-8").decode("unicode_escape")
'ðú'
See here for a more complete answer to your question.
I think this is what you want..
literal_version = '\\xf0'
byte_version = bytes([int('0'+literal_version[1:], 16)])