Given a String, the task is to write a Python program to increment the number which is at end of the string.
Input : test_str = ‘geeks006’
Output : geeks7
Explanation : Suffix 006 incremented to 7.
Input : test_str = ‘geeks007’
Output : geeks8
Explanation : Suffix 007 incremented to 8.
In this, strategy we perform the task of finding number using findall(), then perform the task of separating numeric string and prefix string, then an increment of a numeric string is performed. At last, the string is joined to get a prefix followed by an incremented number.
The original string is : geeks006 Incremented numeric String : geeks61
Method #2 : Using sub() + group() + zfill()
In this, we perform the task of grouping numbers using group() and incrementing, zfill() is used for task of filling the required leading values in numerical. The sub() is used to find the numerical part of strings.
The original string is : geeks006 Incremented numeric String : geeks007
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.