find() a larger string within a smaller string

Discussion in 'Python' started by korean_dave, Nov 14, 2008.

  1. korean_dave

    korean_dave Guest

    stringa = "hi"
    stringb = "hiyoooo"

    I'd like it to return -1 when I do:

    returnVal = stringa.find(stringb);

    Instead, it treats stringa as "hi" and stringb as "hi".

    How do I solve this?
     
    korean_dave, Nov 14, 2008
    #1
    1. Advertising

  2. korean_dave

    Mensanator Guest

    Re: find() a larger string within a smaller string

    On Nov 14, 1:20 pm, korean_dave <> wrote:
    > stringa = "hi"
    > stringb = "hiyoooo"
    >
    > I'd like it to return -1 when I do:
    >
    > returnVal = stringa.find(stringb);
    >
    > Instead, it treats stringa as "hi" and stringb as "hi".
    >
    > How do I solve this?


    Try this:

    >>> stringa = 'hi'
    >>> stringb = 'hiyoo'
    >>> stringa.find(stringb)

    -1
     
    Mensanator, Nov 14, 2008
    #2
    1. Advertising

  3. korean_dave

    John Machin Guest

    Re: find() a larger string within a smaller string

    On Nov 15, 6:20 am, korean_dave <> wrote:
    > stringa = "hi"
    > stringb = "hiyoooo"
    >
    > I'd like it to return -1 when I do:
    >
    > returnVal = stringa.find(stringb);
    >
    > Instead, it treats stringa as "hi" and stringb as "hi".


    You appear to be gravely mistaken:

    | >>> stringa = "hi"
    | >>> stringb = "hiyoooo"
    | >>> returnVal = stringa.find(stringb);
    | >>> returnVal
    | -1

    > How do I solve this?


    You need to tell us why you thought so; then, maybe, we can help.
     
    John Machin, Nov 14, 2008
    #3
  4. korean_dave

    Gary Herron Guest

    korean_dave wrote:
    > stringa = "hi"
    > stringb = "hiyoooo"
    >
    > I'd like it to return -1 when I do:
    >
    > returnVal = stringa.find(stringb);
    >
    > Instead, it treats stringa as "hi" and stringb as "hi".
    >


    No it doesn't. stringb is "hiyoooo" and it "treats" it that way.
    (And just what do you mean by "treat"?)

    > How do I solve this?
    >


    There is nothing to solve. The expression

    stringa.find(stringb)

    asks if "hi" contains "hiyoooo", and since it does not, it returns a -1 indicating so.

    You'll have to describe what you expected and why you expected it before we will be able to see a problem that needs solving. (And then the problem will most likely be in your expectations, not in the find method.)


    Gary Herron



    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Gary Herron, Nov 14, 2008
    #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. Peter Williams
    Replies:
    1
    Views:
    1,395
    Dylan Parry
    Jun 3, 2005
  2. heyo
    Replies:
    3
    Views:
    918
    Dan Pop
    Apr 1, 2004
  3. pete
    Replies:
    4
    Views:
    800
    Dan Pop
    Apr 2, 2004
  4. Matjaz Bezovnik
    Replies:
    4
    Views:
    315
    Robert Kern
    Aug 25, 2009
  5. Replies:
    5
    Views:
    142
    David Dorward
    Feb 9, 2005
Loading...

Share This Page