Python String zfill() Method

The zfill() method in Python is used to pad a string on the left with zeros (0) until it reaches a specified width. This method is particularly useful for formatting strings, such as numbers, to a fixed width, ensuring they align properly when displayed.

Table of Contents

  1. Introduction
  2. zfill() Method Syntax
  3. Understanding zfill()
  4. Examples
    • Basic Usage
    • Handling Negative Numbers
  5. Real-World Use Case
  6. Conclusion

Introduction

The zfill() method allows you to pad a string on the left with zeros until it reaches the specified width. This is useful for formatting strings, particularly numbers, to ensure they have a consistent length, which is often necessary for proper alignment in text-based displays.

zfill() Method Syntax

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

str.zfill(width)

Parameters:

  • width: The desired width of the resulting string. If the specified width is less than or equal to the length of the original string, no padding is added.

Returns:

  • A new string padded with zeros on the left until it reaches the specified width.

Understanding zfill()

The zfill() method pads the original string with zeros on the left until it reaches the desired width. If the original string is longer than the specified width, the original string is returned without any changes. If the string starts with a sign character (+ or -), the zeros are inserted after the sign character.

Examples

Basic Usage

To demonstrate the basic usage of zfill(), we will pad a number represented as a string to a specified width.

Example

text = "42"
padded_text = text.zfill(5)
print("Original text:", repr(text))
print("Padded text:", repr(padded_text))

Output:

Original text: '42'
Padded text: '00042'

Handling Negative Numbers

This example shows how the zfill() method handles negative numbers by inserting zeros after the sign character.

Example

text = "-42"
padded_text = text.zfill(6)
print("Original text:", repr(text))
print("Padded text:", repr(padded_text))

Output:

Original text: '-42'
Padded text: '-00042'

Real-World Use Case

Formatting Numbers for Display

In real-world applications, the zfill() method can be used to format numbers for display, ensuring they have a consistent width for alignment purposes.

Example

numbers = ["1", "42", "123", "4567"]
padded_numbers = [num.zfill(5) for num in numbers]
for num in padded_numbers:
    print(num)

Output:

00001
00042
00123
04567

Preparing Data for Sorting

Another real-world use case is preparing numeric data for sorting by ensuring all numbers have the same length.

Example

numbers = ["3", "20", "100", "5"]
padded_numbers = [num.zfill(3) for num in numbers]
sorted_numbers = sorted(padded_numbers)
print("Sorted numbers:", sorted_numbers)

Output:

Sorted numbers: ['003', '005', '020', '100']

Conclusion

The zfill() method in Python is used for padding strings on the left with zeros to a specified width. By using this method, you can format strings to have a consistent length, which is particularly helpful for aligning text-based displays and preparing data for sorting in your Python applications.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare