What Does Zfill Do in Python | Python String zfill()

Many times while doing some mathematical calculation or some scientific work, we want to fill our string with zeros. If we do it manually, it will take much of our time, and there is a high chance of making some mistakes. So, to do it more precisely and without any distress in Python. We have a special string function available, which is python zfill().

What Python string zfill() basically does is – It adds zeros (0) at the beginning of the string until it reaches the specified length. The point to be noted here is, if the value of the length parameter is less than the length of the string, no filling is done.
After reading the definition, you might have some doubts or concerns. But I am giving you a guarantee that you will become a master in the python zfill() method if you follow the article from beginning to end.

Syntax

string.zfill(length)

Parameters

ParameterDescription
lengthNecessary. It specifies the length of the string we want.

Note: The length of the string parameter must be greater than the current length of the string.

Return Type

Returns a copy of the string with ‘0’ characters padded to the left side of the given string. Pont here to be noted is that the length of the string depends upon the size of the parameter we passed. So there may be two cases in the return type of Python zfill() function.

  1. Suppose the string length is 5, and you passed the value of the parameter as 8. In this case, the python zfill() function will return a copy of the string with three zeros (0) padded at left.
  2. The second condition can be if the parameter’s value is less than or equal to the original string’s length. Then it will return a copy of the original string. And this string will not contain any zeros.

Let’s see some of the examples to get in-depth idea of how python zfill() actually works.

Example 1: Basic Example to Understand How Zfill works in Python

s1 = "Hello World"
print(s1.zfill(12))
print(s1.zfill(13))
print(s1.zfill(14))
print(s1.zfill(15))

Output:

0Hello World
00Hello World
000Hello World
0000Hello World

Explanation:

The above example is pretty straightforward. Here we just initialized a string named s1, which contains the string value “Hello World.” Here the length of the string is 11 (including the white space). In this basic example, we have applied the zfill() function four times. At first, we passed the length parameter as 12. So, in this case, we will get padding of 1 zero at the left or beginning of the string.

Similarly, in the second, third, and fourth print statements, we increased the length parameter value by 1. Therefore, the zfill() function will return ‘Hello World’ with one 0 growing every time on the left side.

Example 2: How Python zfill function Will Work When (length < len of string)

s2 = "Learn Python"
print(s2.zfill(2))
print(s2.zfill(3))
print(s2.zfill(9))
print(s2.zfill(11))

Output:

Learn Python
Learn Python
Learn Python
Learn Python

Explanation:

In this example 2, we tried to see what gonna happen if the value of the parameter is less than or equal to the length of the string. So, in this case, the zfill function is returning the copy of the original string without any zeros. Hence we can conclude that if the value of the parameter is less than the value of the string we will get the original string.

Example 3: Python zfill Function With Sign Characters

num = "-880"
print(num.zfill(10))

txt = "+Hello World"
print(txt.zfill(15))

txt = "-+any+text"
print(txt.zfill(20))

mix = "+++text+num-"
print(mix.zfill(20))

Output:

-000000880

+000Hello World

-0000000000+any+text

+00000000++text+num-

Explanation:

The Python zfill() function works differently if the string starts with a sign (+, -) prefix. In the above example, we have four cases with the string starting with a signed prefix.

  • Case 1: When we have a numeric string, it starts with only one sign character (-). In this case, the zeros will add on after the sign prefix.
  • Case 2: Here, we have a simple string “Hello World” with the sign character (+) at the beginning. So, in this case, the result will be similar to case 1. The zfill() function will return the string’s copy with zeros padded after the sign character.
  • Case 3: In the third case, instead of one sign character at the prefix. We added two sign characters to see what zfill() function return if we add more than one sign character. But the results are quite surprising as the zfill() function returns the copy of the string with zeros padded after the first sign character. Not after the second sign character.
  • Case 4: While playing around with this zfill() function, I wonder what will happen if we place the sign character both at prefix and postfix. But then I got to know about the function quite well. So I can conclude that the zfill function adds the zeros on the left side of the string after the first sign prefix. And it doesn’t care about other sign character at postfix, in-between the string and after the first sign prefix.

Does zfill Support Both Python 2 & 3

The zfill method was introduced for string and Unicode in Python 2.4.1. The release date is 17-MAR-2005. So, we can say. Yes, it supports all the further versions of python 2 &3.

Limitations of zfill Function

It doesn’t matter how good a function is every function has some limitations as in the case of zfill. The three major drawbacks or limitations of the zfill function are:

  1. You can’t pad zeros from the right side or at the postfix of the string.
  2. There might be times when you want to fill zeros according to the current problem, but in the case of zfill, you can’t do that too.
  3. There is some ambiguity when we use zfill with sign characters.

Alternatives to Python String zfill()

Knowing the function in-depth is fine, but there might be various circumstances where the function cannot solve our desired problem. For that case, we have to look for some alternatives to the current function. And also, in the above section, we have seen some of the limitations of the function. So, it’s the need of the hour to find some substitutes. Following are some of the options for Python zfill() Function:

  1. Format Method
  2. string.rjust()
  3. f-strings

Let’s look at the above alternatives briefly. We will talk about them in-depth in some other articles.

PS: I will try to write about these asap. 😛

1. padding Zeros in a string using format method

Str = 'Hello From Python Pool'
x = '{:0>25}'.format(Str)
y = '{:0<25}'.format(Str)
print(x)
print(y)

Output:

000Hello From Python Pool
Hello From Python Pool000

2. Using string.rjust method to add zeros in a string

s3 = "this is string"
print(s3.rjust(20, '0'))

Output:

000000this is string

3. Padding Zeros In A String Using F-strings

Note: Python f-strings are available from Python version 3.6 and above and it only works with real numbers.

s4 = 65656
print(f'{s4:010}')

Output:

0000065656

Can We Pad Zeros from the Right Side of a String in Python Using zfill Function?

Unfortunately, we can’t pad or add zeros from the right end of a given string using the zfill function. But we can use the alternatives to pad zeros from the right of a string in python as shown in the above alternatives. (just, format, etc.)

Python zfill Not Working Problem

While researching why this method is not working for a couple of folks, I have gone through many forums, and I find out that there are two major reasons because of which they cannot use zfill properly:

  1. They are using python version 2.4 or below.
  2. The second thing that I notice is that many of the folks are giving the value of the parameter smaller than or equal to the length of the string. And they are getting the copy of the original string without any zeros padded. This is actually not an error, but that’s how the zfill method works. If (parameter value < len of string), it will return the original string. As a novice programmer, you might think about why there is no zero-padded. But that’s okay; you will learn things step by step.

Also Read

Final Words

The Python zfill is a built-in function that takes the length as an argument and pad zeros from the string’s left. The padding of number zeros depends upon the length of the argument passed in the zfill function.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments