How to pad zeros to a string in Python?

In this article, we will learn to pad zeros to a given string using the Python programming language. Padding zeros can be useful when you have to make a string of desired length. You can do this by adding zeros to the beginning or to the end of the string. In these methods below, in place of zero you can also add any other numbers, string or special characters according to your needs.

Table Of Contents

Make sure to read these articles thoroughly and always try to run these codes in your machine. Here i have used Python 3.10.1 for writing example codes. To check your version write python –version in your terminal.

Pad zeros to a string using format() and F-string

Let’s discuss what are format() and F-string methods and how to use them to pad zeros in a string.

format() method:

format() is the built-in string class which provides formatting options of the strings in python.

SYNTAX :

str.format(value)

It recives only one parameter which is the str/value which needs to formatted.

We can use this method to pad zeros in a string. See the example code below :

CODE :

str_var = "Padding zeros using Format Method {}"

# Pad zeros to the right of string
str_var = str_var.format('0000')

print(str_var)

OUTPUT :

Padding zeros using Format Method 0000

You can see in code above, through format() method we can add zeros at the place of curly braces . You can always position the curly braces according to your requirement.

F-String :

Introduced in Python 3.6, the F-String are also a very fast and less error prone method of formatting strings. Also known as Literal String Interpolation. Lets see an Example code :

EXAMPLE :

zeros = '0000'

str_value = f"Padding zeros using F-String {zeros}" 

print(str_value)

OUTPUT :

Padding zeros using F-String 0000

In code above you can see that by using F-String, we can also pad zeros. Both the F-string and format() method are kind of simillar. Because they both have curly braces which is known as replacement fields.

Pad zeros to a string using zfill() method :

Next we will be using zfill() method to pad zeros to a string. This method takes a number as an argument and adds zeros to the left of the string until the length of the number reaches to the parameter provided. Lets See an Example :

EXAMPLE :

str_var = 'Padding zeros using zfill method.'

# this will print the length of str_var
print(len(str_var)) 

str_var = str_var.zfill(40)
print(str_var)

# this will print the length of str_var
# after padding zeros which should be equal to 40.
print(len(str_var))  

OUTPUT :

34
000000Padding zeros using zfill method.
40

As you can see in the code and output above length of str_var before padding zeros was 34. Parameter 40 has been provided , which means length of the str_var should be 40. So zfill() method padded 6 zeros on the left of str_var.

Pad zeros to a string using ljust(), rjust() and str.center()

Now we will be using some methods of string class to pad zeros to a string. As name suggests of the methods,

  • ljust() method is used align string to left and pad zereos to right.
  • rjust() method is used to align string to right and pad zeros to left.
  • str.center() can be used to align string to center and pad zeros to both left and right of the string.

Above all methods take two arguments :

  • First is width or desired length of the string.
  • Second is character with which you want the empty space to be filled.

Lets see Examples :

ljust() method :

str_var = "Height of Mount Everest is 8,848.86 m."

# pad zeros on the right to make string length 60
str_var = str_var.ljust(60,'0')

print(str_var)

OUTPUT :

Height of Mount Everest is 8,848.86 m.0000000000000000000000

rjust() method :

str_var = "Height of Mount Everest is 8,848.86 m."

# pad zeros on the left to make string length 60
str_var = str_var.rjust(60,'0')

print(str_var)

OUTPUT :

0000000000000000000000Height of Mount Everest is 8,848.86 m.

str.center() method :

str_var = "Height of Mount Everest is 8,848.86 m."

# pad zeros on both right and left to make string length 60
str_var = str_var.center(60,'0')

print(str_var)

OUTPUT :

00000000000Height of Mount Everest is 8,848.86 m.00000000000

You can see in methods above how we can pad zeros and also align zeros according to our needs using ljust(), rjust() and str.center() methods.

Summary

So , we have seen three different methods with which we can pad zeros to a given string in Python Programming language. Using the third method gives a benifit over other two. With ljust() ,rjust() and str.center() we can align our zeros according to our needs. If we need zeros to be padded in between string so we can use the first method, which is format() or F-string method. With the help of curly braces we can place zeros or any other data type at our desired position.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top