string.upto() and string.from()

ikshefem at gmail.com ikshefem at gmail.com
Wed Mar 22 09:41:32 EST 2006


I often need to re-code for myself a small code snippet to define
string.upto() and string.from(), which are used like :

# canonical examples
> "1234456789".upto("45")
'1234'
> "123456dd987".from('d')
'd987'

# if not found, return whole string
> "hello, world !".upto("#")
"hello, world !"
> u"hello, world !".from("#")
u"hello, world !"

> "123456dd987".from('d',2) # second integer argument
'987'

# It can be negative, too
> '192.168.179.131'.upto(".",-1)
"192.168.179"
> "192.168.179.131".from('.',-1)
"131"

# useful example
>  bigstring.upto("\n")
"first line of bigstring"

(I admit I am only using upto, but ...)

Nothing very complicated to make with find and rfind, but wouldn't this
be handy to have it ready in the common string method ?




More information about the Python-list mailing list