In Python 2 (and Python 3) you can do:
number = 1
print("%02d" % (number,))
Basically % is like printf or sprintf (see docs).
For Python 3.+, the same behavior can also be achieved with format:
number = 1
print("{:02d}".format(number))
For Python 3.6+ the same behavior can be achieved with f-strings:
number = 1
print(f"{number:02d}")
Answer from Jack M. on Stack OverflowIn Python 2 (and Python 3) you can do:
number = 1
print("%02d" % (number,))
Basically % is like printf or sprintf (see docs).
For Python 3.+, the same behavior can also be achieved with format:
number = 1
print("{:02d}".format(number))
For Python 3.6+ the same behavior can be achieved with f-strings:
number = 1
print(f"{number:02d}")
You can use str.zfill:
print(str(1).zfill(2))
print(str(10).zfill(2))
print(str(100).zfill(2))
prints:
01
10
100
You can use the zfill() method to pad a string with zeros:
In [3]: str(1).zfill(2)
Out[3]: '01'
The standard way is to use format string modifiers. These format string methods are available in most programming languages (via the sprintf function in c for example) and are a handy tool to know about.
To output a string of length 5:
... in Python 3.5 and above: f-strings.
i = random.randint(0, 99999)
print(f'{i:05d}')
Search for f-strings here for more details.
... Python 2.6 and above:
print '{0:05d}'.format(i)
... before Python 2.6:
print "%05d" % i
See: https://docs.python.org/3/library/string.html
Videos
Without using zfill or format or any other obscure functions.
example:
turn 15 into 0015
or 234 into 0234
I'm building a simple web scraper. The last part of the url is "000001". I need to add this later to make it 000002 and so on. However, if I convert it to an integer, python dumps the leading zeros. Is there any way around this behavior?
An integer stores the value of an integer, and the value of an integer doesn't include leading zeroes. What you want is a specially formatted string representation of your integer like one of these:
>>> x = 33
>>> url = "http://url.com/"
>>> url + format(x, "06.0f")
'http://url.com/000033'
>>> "%s%06.0f" % (url, x)
'http://url.com/000033'
>>> "{}{:06.0f}".format(url, x)
'http://url.com/000033'
It's not just python it's the nature of an integer.
You can use some built in string methods to pad a string to a particular length filling with an arbitrary character like so:
>>> x = "000002" # Set X
>>> x = int(x) + 1 # Convert X to a int and do what ever arithmetic.
>>> print x
3
>>> x = str(x).rjust(6,'0') # Back to String, Right justify padding with '0' to a length of 6
>>> print x
000003
http://docs.python.org/library/stdtypes.html#string-methods
PS. Get as familiar with string methods, and string formatting as you can, they are amazingly useful and very easy to use.
Have fun!
Make use of the zfill() helper method to left-pad any string, integer or float with zeros; it's valid for both Python 2.x and Python 3.x.
It important to note that Python 2 is no longer supported.
Sample usage:
print(str(1).zfill(3))
# Expected output: 001
Description:
When applied to a value, zfill() returns a value left-padded with zeros when the length of the initial string value less than that of the applied width value, otherwise, the initial string value as is.
Syntax:
str(string).zfill(width)
# Where string represents a string, an integer or a float, and
# width, the desired length to left-pad.
Since python 3.6 you can use f-string :
>>> length = "1"
>>> print(f'length = {length:03}')
length = 100
>>> print(f'length = {length:>03}')
length = 001