[SOLVED] How can I add a if statement that goes back in string if there is a number, and to stop if there is something other than a number?

Issue

So let’s say I have a string, like this:

string = '12345+67890'

I want to go back to search for that plus, and add a letter ‘H’, so the end result would be like this:

string = '12345+h67890

Also, what if I want to get the latest +, so if there is two +’s, I want to get the last +?

Any help would be appreciated!

Solution

You could use reverse split to split on the last instance of +, then join with +H:

>>> '+H'.join('123+456+789'.rsplit('+',1))
'123+456+H789'

Answered By – Mark Tolonen

Answer Checked By – Dawn Plyler (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *