Python String zfill() Last Updated : 02 Jan, 2025 Comments Improve Suggest changes 11 Likes Like Report zfill() method in Python is used to pad a string with zeros (0) on the left until it reaches a specified width. In this article, we'll see how zfill() method works. Python s = "42" padded_text = s.zfill(5) print(padded_text) Output00042 Explanation:The string "42" is padded with three zeros on the left to make its total length 5.The zfill() method ensures that the string is right-aligned and padded with zeros.Table of ContentSyntax of zfill() methodExamples of String zfill() methodFrequently Asked Questions (FAQs) on zfill()Syntax of zfill() methodstring.zfill(width)Parameterswidth (required): The total length of the string after padding.If the specified width is less than or equal to the length of the original string, no padding is applied.Return TypeReturns a new string with zeros padded on the left to meet the specified width.Examples of String zfill() method1. Padding a shorter stringLet's see how zfill() behaves with a string shorter than the specified width: Python s = "7" padded_text = s.zfill(3) print(padded_text) Output007 Explanation:The original string "7" has a length of 1.The zfill(3) method pads the string with two zeros on the left to make its total length 3.2. String equal to the specified widthWhat happens when the string length matches the specified width? Python s = "12345" padded_text = s.zfill(5) print(padded_text) Output12345 Explanation:The string "12345" already has a length of 5.No padding is added because the string length matches the specified width.3. String longer than the specified widthLet’s see how zfill() handles a string longer than the specified width: Python s = "Python" padded_text = s.zfill(4) print(padded_text) OutputPython Explanation:The string "Python" has a length of 6, which is greater than the specified width of 4.The method returns the original string without any changes.4. Using zfill() with negative and positive numbersWhen dealing with strings representing numbers, the zfill() method correctly handles the sign: Python #positive and negative numbers s1 = "42" s2 = "-42" print(s1.zfill(5)) print(s2.zfill(5)) Output00042 -0042 Explanation:For "42", zeros are added to the left to make its total length 5.For "-42", the minus sign is preserved, and zeros are added after the sign to maintain the width. Create Quiz Comment P pawan_asipu Follow 11 Improve P pawan_asipu Follow 11 Improve Article Tags : Misc Python python-string Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like