1

For example, if I want to + 1 to the string 'TB004' so it becomes 'TB005'?

To complicate things, the last two digits of 'TB004' must not exceed '12'. once they exceed 12, the last two digits should restart from 00

This is to be looped:

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.
Alex
  • 486
  • 1
  • 7
  • 19

2 Answers2

1

Use the following approach:

s = 'TB011'
ld = int(s[-2:]) + 1
s = '%3s%02d' % (s[:-2], ld if ld <= 12 else 0)

print(s)   # TB012
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
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
Tara Prasad Gurung
  • 3,422
  • 6
  • 38
  • 76
  • 1
    You missed the `should not exceed 12` part – kuro May 02 '17 at 05:55
  • great!! now how about with the complications I threw into my question? – Alex May 02 '17 at 05:56
  • ` 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` if this is what you want – Tara Prasad Gurung May 02 '17 at 06:07