Categories
Python Answers

How to pad zeroes to a string with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *