How to add +1 to a String that contains letters?
例如,如果我想+1到字符串"tb004",那么它就变成"tb005"?
要使事情复杂化,"tb004"的最后两位数字不得超过"12"。一旦超过12,最后两位数应从00重新开始。
这将被循环:
1 2 3 | for i in range(25): #Add 1 to the end of 'TB004', do not exceed 12 as the last two digits, #when you do, restart string with 00 as final numbers. |
使用以下方法:
1 2 3 4 5 | s = 'TB011' ld = int(s[-2:]) + 1 s = '%3s%02d' % (s[:-2], ld if ld <= 12 else 0) print(s) # TB012 |
1 2 3 4 5 6 7 8 9 10 11 | stringhere="TB004" for i in range(25): numberonly=stringhere[2:] intointeger=int(numberonly)+1 if intointeger==12: stringhere="TB000" else: stringhere="TB"+"%03d"%intointeger print stringhere |
号