How to Use String zfill() Method in PythonPython Basics


The zfill() method returns the string of the specified length with zeros added at the beginning of it.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

str = "Have a nice day  !!!"
print ("str.zfill: ",str.zfill(30))
print ("str.zfill: ",str.zfill(10))

Output:

str.zfill:  0000000000Have a nice day  !!!
str.zfill:  Have a nice day  !!!

Syntax

str.zfill(width)

Parameters

Name Description
width The width specifies the length of the returned string with ‘0’ digits filled to the left

Return Value

It returns the string of the specified length.

© 2024 Learnwithgpt.org, all rights reserved. Privacy Policy | Contact Us