This post will discuss how to add padding to a number in Python.

1. Using str.rjust() function

A simple approach to add padding to a number is using the str.rjust() function, which takes the length and the fill character.

Download  Run Code

2. Using str.zfill() function

Another alternative to convert a number to a specified length left-padded with 0’s is using the str.zfill() function.

Download  Run Code

3. Using f-strings

Starting with Python 3.6, you can use f-strings.

Download  Run Code

4. Using str.format() function

Before Python 3.6, you can use general string formatting with the str.format() function.

Download  Run Code

5. Using built-in format() function

Finally, you can use the built-in function format() to add padding to a number.

Download  Run Code

That’s all about adding padding to a number in Python.