157

I'm trying to make length = 001 in Python 3 but whenever I try to print it out it truncates the value without the leading zeros (length = 1). How would I stop this happening without having to cast length to a string before printing it out?

1
  • 1
    It depends how you've got 001. length = 001 is the same as length = 1' (number) and to get 001` you should use some string formatting (see answers). But length = '001' is different, it's a string and you can get number via int(length). Commented Jun 1, 2020 at 11:25

5 Answers 5

216

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.
Sign up to request clarification or add additional context in comments.

2 Comments

Have been coding in python for 5 years and I have never come across this function. Great info.
There's also str(x).rjust(3, '0') which is exactly equivalent in speed and you can pad with any character
184

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

4 Comments

Safer to use print(f'length = {length:>03}') (note the >) as this example would fail if length were a string, eg. length = '1'
For large datasets, note that the top answer (zfill) is moderately faster than this method given an integer input and about 3x faster if your input is already formatted as a string.
What's being used here is Python's formatting mini-language. Here's a cheat sheet with examples: gist.github.com/gauthamchettiar/…
print(f'length = {length:>03}') failing for negative numbers. str(length).zfill(3) working still fine.
52

There are many ways to achieve this but the easiest way in Python 3.6+, in my opinion, is this:

print(f"{1:03}")

4 Comments

Could you please explain this syntax, or link where I may read more about it?
I find this the easiest method. The "3" part dictates the number total digits. so {1:03} would display 001. If it was {123:03} then it would display 123. If it was {123:04} then it would display 0123, etc...
hey that really is the slickest way!
14

Python integers don't have an inherent length or number of significant digits. If you want them to print a specific way, you need to convert them to a string. There are several ways you can do so that let you specify things like padding characters and minimum lengths.

To pad with zeros to a minimum of three characters, try:

length = 1
print(format(length, '03'))

Comments

-8

I suggest this ugly method but it works:

length = 1
lenghtafterpadding = 3
newlength = '0' * (lenghtafterpadding - len(str(length))) + str(length)

I came here to find a lighter solution than this one!

1 Comment

try this: print(f"{1:03}")

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.