🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
- Introduction
zfill()Method Syntax- Understanding
zfill() - Examples
- Basic Usage
- Handling Negative Numbers
- Real-World Use Case
- 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment