0% found this document useful (0 votes)
31 views

Pad String With Zeros in Python Delft Stack

The document discusses different methods to pad strings with zeros in Python including using the zfill, rjust, ljust, format and f-string methods. It provides examples of each method and explains how to use them to add zeros to the beginning or end of a string until it reaches a given length.

Uploaded by

Sathi Natarajan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Pad String With Zeros in Python Delft Stack

The document discusses different methods to pad strings with zeros in Python including using the zfill, rjust, ljust, format and f-string methods. It provides examples of each method and explains how to use them to add zeros to the beginning or end of a string until it reaches a given length.

Uploaded by

Sathi Natarajan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Pad String With Zeros in Python | Delft Stack about:reader?url=https%3A%2F%2Fwww.delftstack.com%...

delftstack.com

Pad String With Zeros in Python


Samyak Jain

4 minutes

1.

2. HowTo

3. Python How-To's

4. Pad String With Zeros in Python

Created: July-27, 2021 | Updated: October-21, 2021

1. Use the zfill() String Method to Pad a String With Zeros in


Python

2. Use the rjust() or ljust() String Method to Pad a String With


Zeros in Python

3. Use the format() to Pad a String With Zeros in Python

4. Use the F-Strings to Pad a String With Zeros in Python

To make a string of the desired length, we add or pad a character


at the beginning or the end of the string until it is of the required
length.

In this tutorial, we will discuss how to pad a string with zeros in


Python.

1 of 5 11/10/22, 15:54
Pad String With Zeros in Python | Delft Stack about:reader?url=https%3A%2F%2Fwww.delftstack.com%...

Use the zfill() String Method to Pad a String With


Zeros in Python

The zfill() method in Python takes a number specifying the


desired length of the string as a parameter and adds zeros to the
left of the string until it is of the desired length. If the number
passed in as the parameter is smaller than the length of the
original string, then there is no change.

For example,

pythonCopy
A = '007'
B = '1.000'
C = '4876'
print(A.zfill(5))
print(B.zfill(6))
print(C.zfill(3))
Output:

textCopy
00007
01.000
4876
In the above example, two zeros were added at the beginning of
string A to make it of length five. Similarly, one zero was added at
the beginning of string B to make it of length six. No changes were
made in string C since the number passed in as a parameter in the
zfill() method is smaller than the length of the original string.

2 of 5 11/10/22, 15:54
Pad String With Zeros in Python | Delft Stack about:reader?url=https%3A%2F%2Fwww.delftstack.com%...

Use the rjust() or ljust() String Method to Pad a


String With Zeros in Python

The rjust() or ljust() method in Python takes a number


specifying the desired length of the string as the required
parameter and a character as an optional parameter.

The rjust() method adds the optional character to the left of the
string until the string is of the desired length. Similarly, the
ljust() method adds the optional character to the right of the
string until the string is of the desired length.

If the character is not specified, the default is space " ".

For example,

pythonCopy
A = 'Hello'
B = 'World'
print(A.rjust(7,"0"))
print(B.ljust(8,"0"))
Output:

textCopy
00Hello
World000

Two zeros were added at the beginning of string A to make it of


length seven using the rjust() method. Similarly, three zeros
were added at the end of string B to make it of length eight using
the ljust() method.

3 of 5 11/10/22, 15:54
Pad String With Zeros in Python | Delft Stack about:reader?url=https%3A%2F%2Fwww.delftstack.com%...

Use the format() to Pad a String With Zeros in


Python

The format() method in Python can format strings to display


them in our desired format based on some pattern. We can use it
to add zeros to the left of the string until the string is of the desired
length. This method can only be used for Python 2 to Python 3.5.

For example,

pythonCopy
A = 'Hello'
B = 'CR7'
print('{:0>8}'.format(A))
print('{:0>5}'.format(B))
Output:

textCopy
000Hello
00CR7

The {:0>8} is the pattern used to pad the string A and increase
its size to 8. Similarly, {:0>5} pads the string B to the desired
length.

Use the F-Strings to Pad a String With Zeros in Python

The f-strings is an addition in recent versions of Python and


provides quick formatting of strings. We can use it to pad a string
with zeros also.

This is similar to the previously discussed method.

For example,

4 of 5 11/10/22, 15:54
Pad String With Zeros in Python | Delft Stack about:reader?url=https%3A%2F%2Fwww.delftstack.com%...

pythonCopy
A = 'Hello'
B = 'CR7'
print(f'{A:0>8}')
print(f'{B:0>5}')
Output:

textCopy
000Hello
00CR7

Related Article - Python String

• Remove Commas From String in Python

• Check a String Is Empty in a Pythonic Way

• Convert a String to Variable Name in Python

• Remove Whitespace From a String in Python

5 of 5 11/10/22, 15:54

You might also like