String question

David Goodger dgoodger at bigfoot.com
Mon Sep 11 22:35:02 EDT 2000


on 2000-09-11 20:53, Dennis Gurnick (Dennis.Gurnick at pandorax.be) wrote:
> Does Python(1.6) have built-in string functions for trimming or extracting
> portion of strings?
> 
> I would like to do the following
> 
> (prototype)
> text = "here is some text"
> seek = "is"
> start = string.find( text, seek )
> theWord = text.grabxxxxx( start, len(seek ) )

You're trying to extract "is" from "here is some text"? You already have
"is" in your example, so I'll assume your intent is more complex. Try this:

    >>> text = "here is some text"
    >>> seek = "is"
    >>> start = text.find(seek)         # you don't need 'string' in 1.6
    >>> theWord = text[start : start + len(seek)]
    >>> theWord
    'is'

Is the slicing operation (sequence[start:end]) what you're looking for?
(Note that a slice includes the start, but does *not* include the end.)

-- 
David Goodger    dgoodger at bigfoot.com    Open-source projects:
 - The Go Tools Project: http://gotools.sourceforge.net
 (more to come!)




More information about the Python-list mailing list