Python string Method - zfill()

https‮/:‬/www.theitroad.com

The zfill() method is a built-in string method in Python that returns a copy of a string with additional zeros (0) added to the left of the string to make it a specified length.

The syntax for using zfill() method is as follows:

string.zfill(width)

Here, string is the original string, and width is the length of the resulting string after adding zeros to the left.

The method returns a new string with additional zeros added to the left of the string to make it a specified length.

Here's an example of using the zfill() method:

string = "42"
zfill_string = string.zfill(5)
print(zfill_string)

Output:

00042

In the example above, the zfill() method was used to add three zeros to the left of the string "42" to make it a total length of 5. The resulting string is stored in the variable zfill_string. Note that if the original string is already longer than the specified width, the method returns a copy of the original string without any changes.