Python

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.

Kuinka näyttää FPS-laskuri Linux-peleissä
Linux-pelaaminen sai suuren työn, kun Valve ilmoitti Linux-tuesta Steam-asiakkaalle ja heidän peleilleen vuonna 2012. Siitä lähtien monet AAA- ja indi...
How to download and Play Sid Meier's Civilization VI on Linux
Introduction to the game Civilization 6 is a modern take on the classic concept introduced in the series of the Age of Empires games. The idea was fai...
How to Install and Play Doom on Linux
Introduction to Doom The Doom Series originated in the 90s after the release of the original Doom. It was an instant hit and from that time onwards th...