Jul-09-2020, 11:27 PM
I don't know a short-and-sweet method to do that. I would probably use a regex, since that easily stores the index of where it finds the first non-digit. But using something like itertools.takewhile or itertools.groupby with str.isdigit could also be used to split the string and rejoin the "integer" parts.
import re s = "435xkb3" if mo := re.match(r"(\d+)", s): #Python 3.8 walrus operator. Split into 2 lines for earlier versions. number = int(mo.group(1)) print(f"The starting number is {number} and the rest begins at index {mo.end()}") print(f"the rest -> {s[mo.end():]}") else: print(f"I couldn't find any integer at the start of the string")