Python String zfill() Method – Tutorial with Examples

Python String zfill() Method

The zfill() method is a built-in method in Python that returns a copy of the string with ‘0’ characters added to the left side of the string until it reaches the specified length. The original string is not modified.

Syntax

The syntax of the zfill() method is as follows:

string.zfill(width)

Here, string is the string that we want to pad with ‘0’ characters and width is the total width of the string after padding. If the width is less than the length of the original string, then the method returns the original string without any modification.

Return Value

The zfill() method returns a copy of the string with ‘0’ characters added to the left side until it reaches the specified width. The original string is not modified.

Examples

Here are three different examples of how to use the zfill() method in Python:

Example 1: Padding a String with ‘0’ Characters

The following example demonstrates how to use the zfill() method to pad a string with ‘0’ characters:

string = "123"
padded_string = string.zfill(5)
print(padded_string)

Output:

00123

In this example, we define a string that contains three characters and then use the zfill() method to add ‘0’ characters to the left of the string until it reaches a width of 5 characters. The resulting string is ‘00123’.

Example 2: Padding a Negative Number with ‘0’ Characters

The following example demonstrates how to use the zfill() method to pad a negative number with ‘0’ characters:

number = -42
padded_number = str(number).zfill(6)
print(padded_number)

Output:

-000042

In this example, we convert the integer -42 to a string and then use the zfill() method to add ‘0’ characters to the left of the string until it reaches a width of 6 characters. Since the number is negative, the ‘-‘ sign is preserved and the resulting string is ‘-000042’.

Example 3: Padding a String with Spaces

The following example demonstrates how to use the zfill() method to pad a string with spaces:

string = "Hello"
padded_string = string.zfill(10)
print(padded_string)

Output:

00000Hello

In this example, we define a string that contains five characters and then use the zfill() method to add ‘0’ characters to the left of the string until it reaches a width of 10 characters. Since the string is shorter than 10 characters, the remaining characters are filled with ‘0’ characters, resulting in the string ‘00000Hello’.

Use Cases

The zfill() method is useful in situations where you need to pad a string with a certain number of characters, such as when working with fixed-width data formats or when you need to align strings in a table. It can also be useful when dealing with negative numbers, as demonstrated in Example 2, where the method preserves the ‘-‘ sign while padding the number with ‘0’ characters.

Another use case for the zfill() method is when generating unique identifiers, such as invoice numbers or order numbers. By padding the identifier with ‘0’ characters, you can ensure that the identifier has a consistent length and is easy to read and compare.

Overall, the zfill() method is a simple and useful tool for padding strings with ‘0’ characters to a specified width. It is easy to use and can save time and effort when working with data that requires fixed-width formatting.

Leave a Reply

Your email address will not be published. Required fields are marked *