Python integer to hex string
<p>Consider an integer 2. I want to convert it into hex string '0x02'. By using python's build-in function hex, I can get '0x2' which is not suitable for my code. Can any one show me how to get what I want in a convenient way. Thank you. </p>

<pre><code>&gt;&gt;&gt; integer = 2
&gt;&gt;&gt; hex_string = format(integer, '#04x')  # add 2 to field width for 0x
&gt;&gt;&gt; hex_string
'0x02'
</code></pre>

<p>See <a href="https://docs.python.org/3.4/library/string.html#format-specification-mini-language" rel="nofollow">Format Specification Mini-Language</a></p>

python hex