Last updated: Apr 8, 2024
Reading timeยท4 min
zfill()
method handles the leading sign prefixTo add leading zeros to a number:
str()
class to convert the number to a string.str.zfill()
method to add leading zeros to the string.num = 246 result_1 = str(num).zfill(5) print(result_1) # ๐๏ธ '00246' result_2 = str(num).zfill(6) print(result_2) # ๐๏ธ '000246'
We used the str() class to convert the number to a string.
This is necessary because adding leading zeros to a number causes a
SyntaxError
.
The str.zfill()
method takes the width of the string and left-fills the string with 0
digits
to make it of the specified width.
num = 13 result_1 = str(num).zfill(3) print(result_1) # ๐๏ธ '013' result_2 = str(num).zfill(4) print(result_2) # ๐๏ธ '0013'
Converting the number 13
to a string gives us a string with a length of 2
.
Passing 3
as the width to the zfill()
method means that the string will get
left-filled with a single 0
digit.
zfill()
method handles the leading sign prefixThe str.zfill()
method handles a leading sign prefix (e.g. +
or -
) by
inserting the padding after the sign.
num = -13 result_1 = str(num).zfill(3) print(result_1) # ๐๏ธ '-13' result_2 = str(num).zfill(4) print(result_2) # ๐๏ธ '-013'
Note that the sign counts toward the width of the string.
If the specified width is less than or equal to the length of the original string, then the original string is returned.
num = 13 result_1 = str(num).zfill(2) print(result_1) # ๐๏ธ '13' result_2 = str(num).zfill(1) print(result_2) # ๐๏ธ '13'
The number in the example has a length of 2
, so trying to fill it to 2
characters doesn't have an effect.
Alternatively, you can use a formatted string literal to add leading zeros to a number.
num = 13 result_1 = f'{num:04}' print(result_1) # ๐๏ธ '0013' result_2 = f'{num:05}' print(result_2) # ๐๏ธ '00013'
fill
value, and the second is the width
of the string.We don't have to use the str()
class to convert the integer to a string as the
conversion is done for us automatically.
Formatted string literals (f-strings) let us include expressions inside of a
string by prefixing the string with f
.
my_str = 'is subscribed:' my_bool = True result = f'{my_str} {my_bool}' print(result) # ๐๏ธ is subscribed: True
Make sure to wrap expressions in curly braces - {expression}
.
Formatted string literals also enable us to use the format specification mini-language in expression blocks.
The first digit after the colon is the fill value and the second is the width of the string.
num = 13 result_1 = f'{num:04}' print(result_1) # ๐๏ธ '0013'
This approach also works if the width of the string is stored in a variable.
num = 13 width_of_string = 4 result_1 = f'{num:0{width_of_string}}' print(result_1) # ๐๏ธ '0013'
You can also use the str.format()
method to add leading zeros to a number.
num = 13 result_1 = '{:04}'.format(num) print(result_1) # ๐๏ธ '0013' result_2 = '{:05}'.format(num) print(result_2) # ๐๏ธ '00013'
fill
value and the second is the width
of the string.The str.format() method performs string formatting operations.
first = 'bobby' last = 'hadz' result = "Name: {} {}".format(first, last) print(result) # ๐๏ธ "Name: bobby hadz"
The string the method is called on can contain replacement fields specified
using curly braces {}
.
You can also use the str.rjust()
method to add leading zeros to a number.
num = 13 result_1 = str(num).rjust(4, '0') print(result_1) # ๐๏ธ '0013' result_2 = str(num).rjust(5, '0') print(result_2) # ๐๏ธ '00013'
The str.rjust() method pads the beginning of the string to the specified width with the provided fill character.
The str.rjust
method takes the following 2 arguments:
Name | Description |
---|---|
width | The total length of the padded string |
fillchar | The fill character to pad the string with |
num = 13 result_1 = str(num).rjust(4, '0') print(result_1) # ๐๏ธ '0013'
The first argument is the width of the padded string and the second is the fill
character (0
in our case).
Notice that we had to convert the number to a string using the str()
class.
This is necessary because rjust()
is a method implemented on strings.
You can also use the format()
function to add leading zeros to a number.
num = 13 result = format(num, '03') print(result) # ๐๏ธ 013 result = format(num, '04') print(result) # ๐๏ธ 0013 result = format(num, '05') print(result) # ๐๏ธ 00013
The first argument the format()
function takes is the value and the second is
a string used to format the value.
The first digit is the fill character (0
) and the second is the total width of
the string.