How to pad zeroes to a string with Python?

Sometimes, we want to pad zeroes to a string with Python.

In this article, we’ll look at how to pad zeroes to a string with Python.

How to pad zeroes to a string with Python?

To pad zeroes to a string with Python, we can use the string’s zfill method.

For instance, we write:

n = '5'
print(n.zfill(3))

We call zfill with 3 to pad the string with 0’s before the number to make it 3 digits long.

Therefore, the print function should print '005'.

Conclusion

To pad zeroes to a string with Python, we can use the string’s zfill method.