TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python String | zfill() method with Examples

Python String zfill() method with Examples on capitalize(), center(), count(), encode(), find(), format(), index(), join(), lower(), ljust(), isupper(), istitle(), isspace(), isprintable(), isnumeric(), islower() etc.

<< Back to PYTHON

Python String zfill() Method

Python zfill() method fills the string at left with 0 digit to make a string of length width. It returns a string contains a sign prefix + or - before the 0 digit.

It returns original length string if the width is less than string length.

Signature

zfill(width)

Parameters

width : length of the string.

Return

It returns a string.

Let's see some examples of zfill() method to understand it's functionality.

Python String zfill() Method Example 1

Let's see an example which has width greater than string length. It returns a new string containing 0 to the left and total length is equal to width.

# Python zfill(width) method 
# Declaring variables
text = "Zfill Example"
# Calling Function
str2 = text.zfill(20)
# Displaying result
print(str2)

Output:

0000000Zfill Example

Python String zfill() Method Example 2

Here, we are passing width less than the string length therefore it returns the original string without filling zeros to the left.

# Python zfill(width) method 
# Declaring variables
text = "Zfill Example"
# Calling Function
str2 = text.zfill(5)
# Displaying result
print(str2)

Output:

Zfill Example

Python String zfill() Method Example 3

This example describes the sign either + or - includes before the zero fills to the left of the string. See every value is filled after prefixing (+ or -) sign.

# Python zfill(width) method 
# Declaring variables
val = "+100"
val2 = "-200"
val3 = "--Rohan--"
# Calling Function
str2 = val.zfill(10)
str3 = val2.zfill(10)
str4 = val3.zfill(10)

# Displaying result
print(str2)
print(str3)
print(str4)

Output:

+000000100
-000000200
-0-Rohan--

Python Tuple Methods


Next TopicPython Strings




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf