I actually was able to get it working a few minutes after posting the question. My apologies for not posting a followup :)<br><br>I ended up using the following format:<br>num = 10<br>num = "%#0.2x"%(num)<br>print(num)<br>
<br>It works, however I'm not sure if it'd be considered very "pythonic" or not.<br>Thanks for your thoughts!<br><br><div class="gmail_quote">On Fri, Nov 5, 2010 at 2:57 PM, Chris Rebert <span dir="ltr"><<a href="mailto:clp2@rebertia.com">clp2@rebertia.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div><div></div><div class="h5">On Fri, Nov 5, 2010 at 11:23 AM, Matty Sarro <<a href="mailto:msarro@gmail.com">msarro@gmail.com</a>> wrote:<br>
> I'm currently trying to convert a digit from decimal to hex, however I need<br>
> the full 4 digit hex form. Python appears to be shortening the form.<br>
> Example:<br>
><br>
> num = 10<br>
> num = "%x"%(num)<br>
> print(num)<br>
><br>
>>a<br>
><br>
> num = 10<br>
> num = "%#x"%(num)<br>
> print(num)<br>
><br>
>>0xa<br>
><br>
> I need it to output as 0x0a, and the exercise is requiring me to use %x to<br>
> format the string. Any help would be appreciated.<br>
<br>
</div></div>Use str.zfill() and add the 0x manually:<br>
<br>
num = 10<br>
hexdig = "%x" % num<br>
padded = hexdig.zfill(2) # pad with 0 if necessary<br>
oxd = "0x" + padded<br>
print(oxd)<br>
<br>
Cheers,<br>
Chris<br>
<font color="#888888">--<br>
<a href="http://blog.rebertia.com" target="_blank">http://blog.rebertia.com</a><br>
</font></blockquote></div><br>