In this tutorial, you will learn all about the Python string zfill() method to add zeros ( 0 ) at the beginning of the string.
This is the best string function to add zeros as leading characters.
In the previous tutorial, we have seen the Python string encode() method to encode the string.
Headings of Contents
Python string zfill() method
Python string zfill
the method is used to add zeros ( 0 ) at the beginning of the string, until specified length.
Syntax
The syntax of Python string zfill() method:
string.zfill(len)
Parameter
The zfill function in Python accepts one parameter.
- len:- Required. A number specifying the position of the element you want to remove.
Note:- If the specified length is less than the length of the string, Then it will not work.
Return Value
zfill() method return new string.
Python string zfill() example
Let’s understand zfill function in Python using different examples.
Example 1:
a = 'Programming Funda'
result = a.zfill(30)
print(result)
Output will be:- ‘0000000000000Programming Funda‘
Example 2:
my_str = 'Python'
result = my_str.zfill(4)
print(result)
The output will be:- ‘Python‘
Conclusion
In this tutorial, you have learned the Python string zfill method to add zeros at the beginning of the string. zfill
method adds zeros only beginning of the string.
I hope this tutorial will help you. if you like this article, please share it with your friends who want to learn python programming.
Other string methods
- String endswith() method
- String casefold() method
- String center() method
- String capitalize() method
- String count() method
- String index() method
- String format() method
- String isalpha() method
- String isidentifier() method
- String islower() method
- String isupper() method
For More Information:- Click Here