Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add leading Zero Python [duplicate]

Tags:

python

string

I was wondering if someone could help me add a leading zero to this existing string when the digits are sings (eg 1-9). Here is the string:

str(int(length)/1440/60) 
like image 918
Evan Gervais Avatar asked Feb 07 '14 06:02

Evan Gervais


People also ask

How do I add a zero in front of a single digit in Python?

Use the str. zfill() Function to Display a Number With Leading Zeros in Python. The str. zfill(width) function is utilized to return the numeric string; its zeros are automatically filled at the left side of the given width , which is the sole attribute that the function takes.

How do you add leading zeros to a string?

The format() method of String class in Java 5 is the first choice. You just need to add "%03d" to add 3 leading zeros in an Integer. Formatting instruction to String starts with "%" and 0 is the character which is used in padding. By default left padding is used, 3 is the size and d is used to print integers.

How to add leading zeros to a string in Python?

In the first example we are using rjust () function to add leading zeros to the string to the total length of 7. In the second example we are using ljust () function to add trailing zeros after the decimal until we get the total length of 7 digit. so the result will be

How to add preceding zeros to the numeric column in pandas?

append or add preceding zeros to the numeric column in pandas python add leading zeros the string in python using zfill () function add preceding and trailing zeros after decimal using rjust () and ljust () function in python add preceding zeros of the character and numeric column using rjust () and ljust () function

How to fill integer or string with preceding zeros in Python?

We will be filling the integer or string with preceding zeros till the desired length is obtained using zfill () function. zfill () Function in Python pads string on the left with zeros to the required width. add preceding zeros of the character and numeric column using rjust () and ljust () function

How to fill the character column with leading zeros in Excel?

with the help of zfill () function we will be filling the character column with leading zeros. We will be taking a column of a dataframe Col1 and applying the zfill () function with length of 10. which will fill the character column with preceding zeros to the total length of 10. so resultant column will be Col1 0000000001


2 Answers

You can use the builtin str.zfill method, like this

my_string = "1" print my_string.zfill(2)   # Prints 01  my_string = "1000" print my_string.zfill(2)   # Prints 1000 

From the docs,

Return the numeric string left filled with zeros in a string of length width. A sign prefix is handled correctly. The original string is returned if width is less than or equal to len(s).

So, if the actual string's length is more than the width specified (parameter passed to zfill) the string is returned as it is.

like image 162
thefourtheye Avatar answered Sep 29 '22 18:09

thefourtheye


Using format or str.format, you don't need to convert the number to str:

>>> format(1, '02') '01' >>> format(100, '02') '100'  >>> '{:02}'.format(1) '01' >>> '{:02}'.format(100) '100' 

According to the str.format documentation:

This method of string formatting is the new standard in Python 3, and should be preferred to the % formatting ...

like image 20
falsetru Avatar answered Sep 29 '22 17:09

falsetru