How to remove leading and trailing zeros in a string? Python

后端 未结 6 2029
庸人自扰
庸人自扰 2020-11-28 06:42

I have several alphanumeric strings like these

listOfNum = [\'000231512-n\',\'1209123100000-n00000\',\'alphanumeric0000\', \'000alphanumeric\']
6条回答
  •  甜味超标
    2020-11-28 07:21

    Did you try with strip() :

    listOfNum = ['231512-n','1209123100000-n00000','alphanumeric0000', 'alphanumeric']
    print [item.strip('0') for item in listOfNum]
    
    >>> ['231512-n', '1209123100000-n', 'alphanumeric', 'alphanumeric']
    

提交回复
热议问题