Pythonic way to remove all 0 from the front of the string

I have a string that will be converted later using int(). These are three digits, from 0 to 3 of them can be 0. How would I split 0s on the left side of the string?

Now I use string.lstrip('0'), but this removes all 0 and makes the string empty, causing an error.

+5
source share
3 answers

You can do it as follows:

s = str(int(s))

Another variant:

s = s.lstrip('0') or '0'
+10
source

For this you want str.lstrip(). But maybe you just need to pass radix to int().

+5
source

how about string[:-1].lstrip('0')?: D

0
source

All Articles