[Tutor] Remove a number from a string

Luis N tegmine at gmail.com
Tue Aug 23 20:59:46 CEST 2005


On 8/23/05, Shitiz Bansal <shitizb at yahoo.com> wrote:
> Hi, 
> Suppose i have a string '347 liverpool street'. 
> I want to remove all the numbers coming at the starting of the string. 
> I can think of a few ways but whats the cleanest way to do it? 
>   
> Shitiz
> 

I believe this question to be rather variable in its answer. If all
your strings are of the form "### word word", then you could safetly
use 'split', as in:

>>> s = '347 liverpool street'

def splitter(s):
    l = s.split()
    try:
        i = int(l[0])
        return i
    except ValueError:
        print "String segment not a number"

>>> splitter(s)
347

But, if your string is also of the form "347-10 liverpool street",
then your problem is more complex.

Luis.


More information about the Tutor mailing list