例如,如果我想对字符串“ TB004”加1,使其变为“ TB005”?
为了使事情复杂化,“ TB004”的后两位数字不能超过“ 12”。一旦超过12,则最后两位数应从00重新开始
这是循环的:
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.
最佳答案
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
关于python - 如何在包含字母的字符串中添加+1?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43730766/