Python Pad String With Zeros

How to pad string with zeros in Python? Adding some characters/values to the string is known as Padding. In Python, zfill() and rjust() methods are used to pad zeros at the beginning of the string and ljust() is used to pad zeros at the end of the string. With the ljust() and rjust() you can fill in any characters but in this article, I will use them to fill in zeros.

Related: Pad String with Zeros

Alternatively, by using f-strings, we can pad zeros to the string in two ways. Using ‘>’, we can pad zeros at left, and using ‘<‘, we can pad zeros at right. The same functionality can be followed by using format() method.

1. Quick Examples of padding zeros to string

Following are quick examples of how to pad or fill zeros to the string to get a specific length of a string.


# Quick Examples

# Consider the string that include 16 characters.
string1 = "sparkby examples"
print("String: ",string1)

# Using zfill()
print(string1.zfill(30))

# Pad left using rjudt()
print(string1.rjust(30, '0'))

# Pad right using ljust()
print(string1.ljust(30, '0'))

# Pad left using f-strings
print(f"{string1:0>30}")

# Pad right using f-strings
print(f"{string1:0>30}'".format(string1))

# Pad right using format()
print('{:0<30}'.format(string1))

2. Pad Zeros to String using rjust()

The rjust() method is Python’s str class that is used to pad zeros on the left of the python string (string is aligned to the right). This method takes two arguments: the minimum width of the string as value, and an optional padding character (which is a space by default).

This method returns a new string with the specified width after padding the fill character to the original string. For example, If your string length is 6, and the value specified inside rfill() is 10, then 4 zeros are padded to the left of the string, so the final length of the string is 10.

2.1 rjust() Syntax

Syntax of the rjust().


# Syntax of rjust()
string1.rjust(value, '0')

2.2 rjust() Parameters

  1. value is the first parameter that specify the total length of the string after padding zeros.
  2. Second parameter takes the element that has to be padded to the string. In our case, it is ‘0’.

2.3 Pad Zeros to the Left of the String

Let’s create a string and pad the zeros at the start (left of the string).


# Consider the string that include 6 characters.
string1 = "PYTHON"
print("String: ",string1)

# Pad 4 zeros to the left to get string length 10
print(string1.rjust(10, '0'))

# Output:
# String:  PYTHON
# 0000PYTHON

3. Pad Zeros to String using ljust()

The ljust() method is also Python’s str class that is used to pad zeros on the right of the python string (string is aligned to the left). This method also takes two arguments: the minimum width of the string as value, and an optional padding character (which is a space by default).

If your string length is 7, and the value specified inside rfill() is 10, then 3 zeros are padded to the right of the string, so the final length of the string is 10.

3.1 ljust() Syntax

Following is the syntax of the ljust()


# Syntax of ljust()
string1.ljust(value, '0')

3.2 ljust() Parameters

  1. value is the first parameter that specifies the total length of the string after padding zeros.
  2. Second parameter takes the element that has to be padded to the string. In our case it is ‘0’.

3.3 Example

By using ljust() method, let’s pad zeros at the end of the string.


# Pad 4 zeros to the right to get string length 10
print(string1.ljust(10, '0'))

# Output:
# PYTHON0000

4. Pad Zeros to String using zfill()

The zfill() is used to fill the string with zeros at the start of the string (left of the string). It takes the value as a parameter that specifies the total length of the string along with zeros. By default, zfill() treats the string as a numeric value and removes any leading plus or minus sign. If you want to preserve the sign, you can pass the sign parameter as '+' or '-'

If you have a string length of 6 and specify 10 as the length, zfill() returns a string with the length 10 that includes 4 zeroes on the left.

4.1 zfill() Syntax

Let’s look at the syntax of zfill()


# Here, string1 is the input string.
string1.zfill(value)

4.2 Example

Here, let’s use the zfill() to fill the zeros on the left of the string.


# Use zfill() to pad zeros
print(string1.zfill(10))

# Output:
# 0000PYTHON

5. Pad Zeros to String using f-strings

The f-strings in python are used to display the strings in the desired formats. It can be possible to pad zeros at Left and right with > and < operators.

  1. If we specify element > value, zeros are padded at the start of the string (left padding).
  2. If we specify element < value, zeros are padded at the end of the string (right padding).

The value specified will be equal to the length of the string after padding.

5.1 f-strings Syntax

Let’s look at the syntax of f-strings with left and right paddings.


# Syntax of f-strings
# Specify element as 0 to pad zeros.

# Syntax of Left padding
f"{string1:element > value}"

# Syntax of Right padding
f"{string1:element < value}"

5.2 Example

Create a string with 6 characters and pad zeros by specifying different values at Left and Right using f-strings.


# Consider the string that include 7 characters.
string1 = "PYTHON"
print("String: ",string1)

# Pad left
print(f"{string1:0>10}")

# Pad right
print(f"{string1:0<10}")

# Output:
# String:  PYTHON
# 0000PYTHON
# PYTHON0000

Explanation:

  • First example – 4 zeros are Left padded by specifying the value as 10.
  • Second example – 4 zeros are Right padded by specifying the value as 10.
  • In both outputs, the length of the final string is 10, as the value is 10.

6. Pad Zeros to String using format()

Finally, format() in python is used to display the strings in the desired formats similar to f-strings. It can be used to pad zeros at Left and Right with > and < operators. But we need to specify this inside {}.

The value specified will be equal to the length of the string after padding.

6.1 format() Syntax

Let’s look at the syntax of format() with Left and Right Paddings.


# Syntax of format()
# Specify element as 0 to pad zeros.

# Syntax of Left padding
'{:element > value}'.format(string1)

# Syntax of Right padding
'{:element < value}'.format(string1)

6.2 Example

Let’s use the ‘{}’ .format() to pad or fill zeros on the left and right of the string in python.


# Consider the string that include 6 characters.
string1 = "PYTHON"
print("String: ",string1)

# Pad left
print('{:0>10}'.format(string1))

# Pad right
print('{:0<10}'.format(string1))

# Output:
# String: PYTHON
# 0000PYTHON
# PYTHON0000

7. Conclusion

In this article, you have learned zfill() and rjust() is used to fill or pad the zeros on the left of the string, and ljust() is used to fill on the right of the string in Python. Alternatively, you can also use f-string and format() to fill the zeros on the string to get the desired width of the stirng.

Leave a Reply