zfill() in Python | Python String zfill() Method

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The zfill() method in Python Programming returns a String by adding zeros (0) to the left of the string to reach the specified length that is mentioned in its parameters. The copy of the string is returned with zeros (0) being padded at the beginning of the given string.

Syntax of zfill() function in Python

Where string represents a string under consideration and width refers to the length of the output string with zeros being padded at the very beginning.

Parameters of zfill() function in Python

  • width - It is mandatory to give the length of the string, which is represented by the number reflecting the desired length of the string. This width specifies the length of the returned string after the zfill() method has been applied.

Return Values of zfill() function in Python

Return Type: str

zfill in Python returns a copy of the string with '0' characters added to the left side of the string to match with the width defined in the zfill() method.

Let us consider the below scenarios to understand better :

  • If the initial length of the string is 20 and we specify the width 25. Then, in this case, the zfill() method will return the copy of the string with five '0' added to the left.
  • If the initial length of the string is 20 and we specify the width 18. Then, in this case, the zfill() method will return only the original copy of the string, and the length of the returned string will also be 20, the same as before.

Exceptions of zfill() function in Python

  • In the zfill() method, if the string has the sign prefix '+' or -, it will return a string with 0 being padded after the first sign prefix.

Example of zfill() function in Python

Code:

Output:

Also, it is important to note here that if the value of the width parameter is less than the length of the string, then no padding is done on the string, and a copy of the original string is returned.

What is zfill() function in Python?

Python's str class provides several methods to perform some common operations on a string.

One such method is zfill(). zfill() method is used for adding zeros (0) at the very beginning of the given string and returning this copy of the string.

Note:

  • If the string starts with a sign prefix like ('+', '-'), then the 0 digits are padded after the first sign prefix character.
  • zfill() takes only integer as width.
  • The width is specified as the length of the returned string after the zfill() method has been applied, that is, with 0 digits filled at the very beginning.

More Examples

Example 1: How zfill() works with Sign Prefix?

If the string starts with a sign prefix like ('+', '-'), then the 0 digits are padded after the first sign prefix character.

Code:

Output:

Explanation:

This is the usual behaviour of the zfill() function in Python, as discussed above, and it will add zeros from the left side only after the first sign prefix to match the width of the zfill() function in python.

Example 2: Using zfill() function with width greater than string length

Code:

Output:

Explanation:

This zfill() function in Python for width greater than string length will always return the copy of the original string with zeros added to the left side.

Example 3: Using zfill() function with a width lesser than the string length

Output:

Explanation:

This zfill() function in Python for width lesser than string length will always return the original string with no zeros added to the left side.

Conclusion

  • The zfill() method of the str class returns the string by adding zeros (0) to the left to reach the specified length mentioned in its parameters.
  • zfill() function does modify the original string, and returns a new string with zeroes (0) added.
  • If the value of the width parameter is less than the length of the string, then no padding is done on the string, and the original string is returned.

See Also