← Back to challenges

String Incrementer

PythonHardstringsarrays

Instructions

Write a function which increments a string to create a new string.

  • If the string ends with a number, the number should be incremented by 1.
  • If the string doesn't end with a number, 1 should be added to the new string.
  • If the number has leading zeros, the amount of digits should be considered.

Examples

increment_string("foo") ➞ "foo1"

increment_string("foobar0009") ➞ "foobar0010"

increment_string("foo099") ➞ "foo100"

Notes

N/A

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.