Home > other >  Python regex how to remove all zeo from beginning?
Python regex how to remove all zeo from beginning?

Time:10-16

I have lot of string somethings like this "01568460144" ,"0005855048560"

I want to remove all zero from beginning. I tried this which only removing one zeo from beginning but I also have others string those have multiple zeo at the beginning.

re.sub(r'0','',number)

so my expected result will be for "0005855048560" this type of string "5855048560"

CodePudding user response:

If the goal is to remove all leading zeroes from a string, skip the regex, and just call .lstrip('0') on the string. The *strip family of functions are a little weird when the argument isn't a single character, but for the purposes of stripping leading/trailing copies of a single character, they're perfect:

>>> s = '000123'
>>> s = s.lstrip('0')
>>> s
'123'

CodePudding user response:

>>> v = '0001111110'
>>> 
>>> str(int(v))
'1111110'
>>> 
>>> str(int('0005855048560'))
'5855048560'
  • Related