pythonstringincrementchr

Python - Increment last character in a String by 1


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?


Solution

  • Below is the sample code that would work.

    value = 'Run1'
    value = value[:-1] + str(int(value[-1])+1)
    print(value)
    'Run2'