例如,如果我想要字符串'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.
答案 0 :(得分:1)
使用以下方法:
s = 'TB011'
ld = int(s[-2:]) + 1
s = '%3s%02d' % (s[:-2], ld if ld <= 12 else 0)
print(s) # TB012
答案 1 :(得分:0)
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