str(int_var) formatted

MRAB python at mrabarnett.plus.com
Fri Oct 29 13:30:39 EDT 2010


On 29/10/2010 17:18, "none <"@mail.python.org wrote:
> On 29/10/10 16:59, Tracubik wrote:
>> Hi all,
>> i've to convert integer x to string, but if x< 10, the string have to be
>> '0x' instead of simple 'x'
>>
>> for example:
>>
>> x = 9
>> str(x) --> '09'
>>
>> x = 32
>> str(x) --> '32'
>>
>> x represent hour/minute/second.
>>
>> I can easily add '0' with a if then block, but is there a built-in way to
>> add the '0' automatically?
>>
>> Thanks
>> Nico
>
> Python2: "%02d" % x
> Python3: format(x,"02d")

Also:

Python 3: "{:02}".format(x)



More information about the Python-list mailing list