سؤال

I know that to pad a string you do something like:

>>> n = '6'
>>> print n.zfill(3)
>>> '006

and a number:

>>> n = 6
>>> print '%03d' % n
>>> 006
>>> print "{0:03d}".format(6)  # python >= 2.6
>>> 006
>>> print("{0:03d}".format(6))  # python 3
>>> 006

My question is: Is there a way to undo/un/de-pad a string and number?

هل كانت مفيدة؟

المحلول

Yes. You can simply do:

your_str.lstrip('0')

Examples

>>> '00003'.lstrip('0')
'3'

>>> '0234782394000'.lstrip('0')
'234782394000'

Note

lstrip does not get rid of zeroes at the end of the string:

>>> '00100'.lstrip('0')
'100'
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top