0

I've searched on how to do this in python and I can't find an answer. If you have a string:

Value = Run1

How would you increment last digit in a string by 1? So the input that I'm looking for is:

Value = Run2

I know I can do it with one character using ord and chr. Is there a way to do this with string?

Vishal K
  • 11
  • 3

2 Answers2

1

Below is the sample code that would work.

value = 'Run1'
value = value[:-1] + str(int(value[-1])+1)
print(value)
'Run2'
Abhi
  • 1,080
  • 1
  • 7
  • 21
1

I assume by increment, you want to do it multiple time

Maybe this will help you, so I separate prefix and the integer

value = "Run1"
# I need to hardcode in this, maybe someone else know how to parse this
prefix = value[:-1]
current_i = int(value[-1])
loop = 10
for i in range(loop):
    print(prefix+str(current_i))
    current_i+=1

Result

Run1
Run2
Run3
Run4
Run5
Run6
Run7
Run8
Run9
Run10
d_frEak
  • 440
  • 3
  • 12