string.upto() and string.from()

Fredrik Lundh fredrik at pythonware.com
Wed Mar 22 10:22:33 EST 2006


ikshefem at gmail.com wrote:

> 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 ?

somewhat related:

http://www.python.org/dev/summary/2005-08-16_2005-08-31.html#str-find

</F>






More information about the Python-list mailing list