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
How would you add zeros to the beginning of an Integer, such that there are 4 total digits in the number?
Python-Turning a string into and integer while keeping leading zeros - Stack Overflow
Bug! Problem in integer datatype
python - Display number with leading zeros - Stack Overflow
Integers are stored/displayed as a "normal" number, not the string value. If you want to display the number prefixed with zeroes you can do that when displaying the variable.
You can use the following syntax to add leading zeros to an integer:
print "%010d" % (241034812,)
The example above will display the integer 241034812 with 10 digits as 0241034812.
I'm setting the variable 'num' to '0241034812'
No, you're setting it to 241,034,812: an integer value, about two hundred forty million. If you want to set it to '0241034812', you should use a string rather than an integer. That is, you should drop the call to int:
>>> num = '0241034812'
>>> print(num)
0241034812
>>>
Without using zfill or format or any other obscure functions.
example:
turn 15 into 0015
or 234 into 0234
You cannot have leading zeros at all in python3 and in python 2 they signify octal numbers. If you are going to pass it into a string just keep it as a string.
If you want to pad with 0's you can zfill:
print(x[2:].zfill(5))
I don't see why you slice at all thought when you seem to want the exact same output as the original string.
format_string=r'/home/me/Desktop/File/{}.txt'.format(x)
will give you exactly what you want.
If I understand you correctly, you're going to be using the int in a string, correct? Since this is the case, you should do exactly what you're doing:
>>> x = 1
>>> format_string = r'/home/me/Desktop/File/AB%04d.txt' % x
>>> print(format_string)
/home/me/Desktop/File/AB0001.txt
>>>
You don't need to store the variable as an int with the format 001 (which you can't do anyway). You convert it to that format when you create format_string.
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}")
You can use str.zfill:
print(str(1).zfill(2))
print(str(10).zfill(2))
print(str(100).zfill(2))
prints:
01
10
100
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!
I'm currently learning Python through Harvard's free CS50 course, and while working on the Outdated assignment, I noticed something very bizarre in this specific part below:
while True:
try:
date = input("Date: ")
if date.find("/"):
m, d, y = date.split("/")
print(f"{y}-{m:02}-{d:02}")
except EOFError:
print("")
break
The leading zeros in the identifiers inside the print function result with zeros being in the end, as opposed to the left. For instance, if m is 4, in this case, m will result in 40, instead of 04. In another part of the script using a similar method, this does not occur. Is there something I'm doing wrong? Sorry if its a stupid question, I'm still learning.