Cleanest way to obtain the numeric prefix of a string

Viewed 2372

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, and I suppose that it is hardly measurable in Python anyway.

For example:

Given the string '123abc456def', what is the cleanest way to obtain the string '123'?

The code below obtains '123456':

input = '123abc456def'
output = ''.join(c for c in input if c in '0123456789')

So I am basically looking for some way to replace the if with a while.

9 Answers
Related