Isn't there a substring(start, end)-function????

Discussion in 'Python' started by Dave, Aug 6, 2003.

  1. Dave

    Dave Guest

    Hi all,

    Am I blind, or what? I can't find any quick way to do the following in
    Python:

    substring(beginIndex, endIndex) witch returns the substring between
    beginIndex and endIndex.

    Like:
    text = "If someone attacks you with a banana"
    print text.substring(0,3)
    Should print "If "

    I've found absolutely everything else that I expect from a modern
    programming language, but none of the modules (not even "string"!)
    seems to have what I'm looking for.

    Please tell me I'm blind!

    Dave
     
    Dave, Aug 6, 2003
    #1
    1. Advertising

  2. (Dave) writes:

    > Hi all,
    >
    > Am I blind, or what? I can't find any quick way to do the following in
    > Python:
    >
    > substring(beginIndex, endIndex) witch returns the substring between
    > beginIndex and endIndex.
    >
    > Like:
    > text = "If someone attacks you with a banana"
    > print text.substring(0,3)
    > Should print "If "
    >
    > I've found absolutely everything else that I expect from a modern
    > programming language, but none of the modules (not even "string"!)
    > seems to have what I'm looking for.
    >
    > Please tell me I'm blind!


    Well, you're just not looking in the right place:

    >>> text = "If someone attacks you with a banana"
    >>> text[0:3]

    'If '

    I'm pretty sure this is in the tutorial -- the key word you're looking
    for is "slice".

    Cheers,
    mwh

    --
    [2. More type system hacking --- text/plain; type-argh.diff]
    -- csr on sbcl-devel
     
    Michael Hudson, Aug 6, 2003
    #2
    1. Advertising

  3. Dave wrote:

    > Am I blind, or what? I can't find any quick way to do the following in
    > Python:
    >
    > substring(beginIndex, endIndex) witch returns the substring between
    > beginIndex and endIndex.
    >
    > Like:
    > text = "If someone attacks you with a banana"
    > print text.substring(0,3)
    > Should print "If "


    You're not blind but you've overlooked string slicing:

    C:\> python
    Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> text = "If someone attacks you with a banana"
    >>> print text[0:3]

    If
    >>> print text[3:10]

    someone
    >>> print text[-6:]

    banana
    >>> print text[::-1]

    ananab a htiw uoy skcatta enoemos fI


    more info here: http://www.python.org/doc/current/ref/slicings.html

    --Irmen
     
    Irmen de Jong, Aug 6, 2003
    #3
  4. > You are ;-) You couldn't find it in the documentation for the modules
    because
    > it's a base operator on the string itself:


    Surely a lesson to be learned there?

    I am still a relative newcomer and regularly find myself stumbling round the
    tutorial to find the answers that are too easy for the library reference.

    Christopher Boomer.
     
    Christopher Boomer, Aug 6, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Harvey Thomas
    Replies:
    0
    Views:
    392
    Harvey Thomas
    Aug 6, 2003
  2. Noam Raphael
    Replies:
    17
    Views:
    543
    Noam Raphael
    May 26, 2004
  3. Replies:
    70
    Views:
    1,735
    Flash Gordon
    Jul 21, 2007
  4. KONTRA Gergely

    substring: to the end of the string

    KONTRA Gergely, Nov 14, 2003, in forum: Ruby
    Replies:
    16
    Views:
    219
    Jason DiCioccio
    Nov 18, 2003
  5. Replies:
    3
    Views:
    231
    Sherm Pendley
    Aug 3, 2005
Loading...

Share This Page