What is the cleanest way to obtain the numeric prefix of a string in Python?
By \"clean\" I mean simple, short, readable. I couldn\'t care less about performance, a
One way, but not very efficient since it works through the whole string without break
would be:
input_string = '123abc456def'
[input_string[:c] for c in range(len(input_string)) if input_string[:c].isdigit()][-1]
This appends each substring with increasing size if it is a digit and then appends it. So the last element is the one you look for. Because it is the longest startstring that is still a digit.