How Can We Help?
In this tutorial, we will explain how to use Python string zfill() method with basic syntax by example for better understanding.
The String zfill() method returns an original string value with filled zero at the left-hand side of the string value to justify the string’s new length (width).
Syntax:
<string_value/string_variable>.zfill(width)
Input Parameters:
- string_value / String variable : The string that we want to fill zeros at the left-hand side to justify it with a specific length.
- width : The length character to reach with padding-left zero to the string value..
Python String Fill 0 at left-hand side
We Can fill zero at the left-hand side of a string value to match a specific length using python’s zfill method. it takes one parameter, which is the width of the string value, to reach after fill zeroes to the left side.
Example
# The String Value That We Want To Fill Zero to the left-hand side.
str1='python12'
#Print String After fill zero at the beginning of the string value
print('Zero fill: ',str1.zfill(10))
Output of example
Zero fill: 00python12
In the above example, we have changed the length of the string value of string variable ‘str1’ from 8 to 10 and justified this length; we have added ‘0’ at the left-hand side of the original string, so the output shows as ’00python12′.
Python String zfill at right-hand Side
To fill zero at the right-hand side of a string value in Python using the zfill function, unfortunately, the zfill won’t fit our need here, but there are multiple solutions to achieve this. The first option to do this is to use the ljust function or to use string.format() method.
Example (1)
# The String Value That We Want To Fill Zero to the right-hand side.
str1='222'
#Print String After fill zero at the end of the string value
print('Zero fill at right: ',str1.ljust(10,'0'))
Output of example
Zero fill at right: 2220000000
Example (2)
# The String Value That We Want To Fill Zero to the right-hand side.
str1='222'
#Print String After fill zero at the end of the string value
print('Zero fill at right: ','{:<010}'.format(str2))
Output of example
Zero fill at right: 2220000000
In the above example, we have changed the length of the string value of string variable ‘str1’ from 3 to 10 and justified this length; we have added ‘0’ at the right-hand side of the original string, so the output shows as ‘2220000000’ by using format method. and we will cover this format method in more details in coming articles. And you can refer to Format Specification Mini-Language part from python docs from here.
In this tutorial, you have learned the string zfill() method in Python. What is it? And how to fill zero to the beginning of a string to match a specific length.
Hopefully, it was clear and concise.
If you have an inquiry or doubt, don’t hesitate to leave them in a comment. We are waiting for your valuable feedback.
Similar Python String Formatting Methods:
- title() : It returns the “title cased” version of string means all the words start with uppercase and the remaining are lowercase.
- capitalize() : It capitalizes the first character of the String.
- center() : It returns a space-padded string with the original string centered to a total of width columns.
- swapcase() : It inverts the case of all the characters in a string.
- upper() : It converts all the characters of a string to uppercase.
- lower() : It converts all the characters of a string to lowercase.
- strip() : It removes particular characters of a string from left and left side.
- rstrip() : It removes particular character of a string from right side.
- lstrip() : It removes particular character of a string from left side.
- rjust() : It returns an original string with a filled specific character or space at the right side of the string value to justify the string’s new length (width).
- ljust() : It returns an original string with a filled specific character or space at the left side of the string value to justify the string’s new length (width).
Related Articles
Python String – Mastering By Practical Examples (Comprehensive Guide)
Python Tutorials List:
- Python tutorials list : access all python tutorials.