How to increment a value with leading zeroes?

后端 未结 7 1476
一整个雨季
一整个雨季 2021-02-05 22:23

What would be the best way to increment a value that contains leading zeroes? For example, I\'d like to increment \"00000001\". However, it should be noted that the number of

7条回答
  •  情歌与酒
    2021-02-05 22:50

    Presumably, you specifically mean an integer represented as a string with leading zeros?

    If that's the case, I'd do it thusly:

    >>> a
    '00000000000000099'
    >>> l = len(a)
    >>> b = int(a)+1
    >>> b
    100
    >>> ("%0"+"%dd" % l) % b
    '00000000000000100'
    

提交回复
热议问题