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.



from Linux Hint https://ift.tt/3o1l2xJ

Post a Comment

0 Comments