piton

Python zfill() Function

Python zfill() Function
Owing to the versatility and availability of vast built-in modules, functions, and statements, Python is now a widely-used general-purpose programming language. The Python built-in features help programmers to perform complicated tasks very simply and efficiently. The zfill() is a Python built-in function that zeros on the left of the string to fill the specified width and return the string's copy. It makes a padded string by adding zeros. This article demonstrates the use of the Python zfill() function.

Syntax

First, let's discuss the syntax of the zfill() function. The syntax of the zfill() function is as follows:

str_name.zfill(width)

The zfill() function takes the width as an argument and adjusts the zero at the left side of the string according to the specified width. The width can also be considered as the length of the string.

Example1: Using zfill() function

For example, a string contains three characters; it means that the original width of the string is 3. When we call the zfill() function and specify the width 15, then it will add 12 zeros add the left side of the string to fill the width. Whitespace also adds up in width. Let's see an example of it. The width of the string 'hello' is 5 originally.

#defining a string
my_str = 'hello'
#using zfill() function
print(my_str.zfill(10))

Output

Five zeros are added on the left side of the string.

Now let's add two whitespaces in our string and make it 'he ll o'. Now, the original width of the string is 7.

#defining a string
my_str = 'he ll o'
#using zfill() function
print(my_str.zfill(10))

Output

Let's see another example of the zfill() function.

#defining a string
my_str = '10'
print("The original string is:",my_str)
#using zfill() function
print("The zfill() function returned string is: ",my_str.zfill(10))

Output

The 8 zeros are added.

Example2: Using zfill() function

If we pass the width to zfill() function less than the original width of the string, then nothing will happen. Let's see an example of it.

In the below given an example, the original length or width of the string is 9. In the zfill() function, we have specified width 3. In this case, neither does it add zeros on the left side nor shows an error.

#defining a string
my_str = 'linuxhint'
print("The original string is:",my_str)
#using zfill() function
print("The zfill() function returned string is: ",my_str.zfill(3))

Output

Example3: Using zfill() function with sign prefix

The zfill() function works differently if the string starts with a sign prefix. It adds the zeros on the left side of the string after the first sign prefix. Let's see an example.

#defining a string
my_str = '+linuxhint'
print("The original string is:",my_str)
#using zfill() function
print("The zfill() function returned string is: ",my_str.zfill(13))
my_str = '+10'
print("The original string is:",my_str)
#using zfill() function
print("The zfill() function returned string is: ",my_str.zfill(13))
my_str = '--20'
print("The original string is:",my_str)
#using zfill() function
print("The zfill() function returned string is: ",my_str.zfill(13))

Output

Conclusion

The zfill() is the Python built-in function that takes the width as an argument and fills the zeros on the left side of the string according to the specified width. This article discusses the Python zfill() function in detail.

Cursor jumps or moves randomly while typing in Windows 10
If you find that your mouse cursor jumps or moves on its own, automatically, randomly while typing in Windows laptop or computer, then some of these s...
How to reverse Mouse and Touchpads scrolling direction in Windows 10
Mouse and Touchpads not only make computing easy but more efficient and less time-consuming. We cannot imagine a life without these devices, but still...
How to change Mouse pointer and cursor size, color & scheme on Windows 10
The mouse pointer and cursor in Windows 10 are very important aspects of the operating system. This can be said for other operating systems as well, s...