Add escape characters to a variable in Python
<p>I need to add an escape character to a variable which I'm appending to another string and have it apply its effects. This is what I have:</p>

<pre><code>h1 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
h2 = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']

h3 = list(itertools.product(h1, h2))
h4 = []

for item in h3:
    h4.append(''.join(item))

temp = r'\x' + str(h4[0]) + '\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c'
</code></pre>

<p>So if i have \xh I need the character with hex value hh but I can't seem to find anything in python that does this besides \x which I can't seem to use on variables.</p>

<p>Any suggestions?</p>

<p>Use <code>int()</code> to convert your hex value to an integer and then <code>chr()</code> to convert that number to a character:</p>

<pre><code>import itertools

hexdigits = "123456789abcdef"

for dig1, dig2 in itertools.product(hexdigits, hexdigits):
    char = chr(int(dig1 + dig2, 16))
    temp = char + '\x7e\x15\x16\x28\xae\xd2\xa6\xab\xf7\x15\x88\x09\xcf\x4f\x3c'
</code></pre>

python