string find mystery

Discussion in 'Python' started by Helvin, Sep 3, 2009.

  1. Helvin

    Helvin Guest

    Hi,

    I have come across this very strange behaviour. Check this code:

    if file_str.find('Geometry'):
    #if file_str.endswith('Data_Input_Geometry.txt'):
    print 'I found geometry'
    elif file_str.find('Material'):
    print 'I found material'
    The amazing thing is when file_str = 'C:\Qt\SimLCM\Default
    \Data_Input_Material.txt',
    the first if statement if fulfilled, that seemingly says that in this
    file_str, python actually finds the word 'Geometry'.
    I know this, because the line: 'I found geometry' is printed. However,
    if instead of using file_str.find(), I use file_str.endswith(), it
    does not exhibit this strange behaviour.

    Obviously, I want the elif line to be true, instead of the first if
    statement.

    Does anyone know why this is happening?
    Help much appreciated!

    Very puzzled,
    Helvin
    Helvin, Sep 3, 2009
    #1
    1. Advertising

  2. Helvin

    Sean DiZazzo Guest

    On Sep 2, 10:10 pm, Helvin <> wrote:
    > Hi,
    >
    > I have come across this very strange behaviour. Check this code:
    >
    >         if file_str.find('Geometry'):
    >         #if file_str.endswith('Data_Input_Geometry.txt'):
    >             print 'I found geometry'
    >         elif file_str.find('Material'):
    >             print 'I found material'
    > The amazing thing is when file_str  = 'C:\Qt\SimLCM\Default
    > \Data_Input_Material.txt',
    > the first if statement if fulfilled, that seemingly says that in this
    > file_str, python actually finds the word 'Geometry'.
    > I know this, because the line: 'I found geometry' is printed. However,
    > if instead of using file_str.find(), I use file_str.endswith(), it
    > does not exhibit this strange behaviour.
    >
    > Obviously, I want the elif line to be true, instead of the first if
    > statement.
    >
    > Does anyone know why this is happening?
    > Help much appreciated!
    >
    > Very puzzled,
    > Helvin


    string.find() returns the index at which the given word is found
    within the string. If the string is not found it returns -1. So, no
    matter what you do, string.find() will evaluate to "True"

    You could use it like this: if file_str.find("Geometry") != -1:

    but you probably want to use: if "Geometry" in file_str:

    ~Sean
    Sean DiZazzo, Sep 3, 2009
    #2
    1. Advertising

  3. Helvin

    John Yeung Guest

    On Sep 3, 1:10 am, Helvin <> wrote:
    >         if file_str.find('Geometry'):
    >         #if file_str.endswith('Data_Input_Geometry.txt'):
    >             print 'I found geometry'
    > The amazing thing is when file_str  = 'C:\Qt\SimLCM\Default
    > \Data_Input_Material.txt',
    > the first if statement if fulfilled, that seemingly says that in this
    > file_str, python actually finds the word 'Geometry'.
    >
    > Does anyone know why this is happening?


    Yep. You should read the documentation on the find method. It
    returns -1 when the substring is not found. Also, if 'Geometry' had
    been found at the beginning, it would have returned 0.

    John
    John Yeung, Sep 3, 2009
    #3
  4. Helvin

    John Yeung Guest

    On Sep 3, 1:45 am, Sean DiZazzo <> wrote:
    > string.find() returns the index at which the given word is found
    > within the string.  If the string is not found it returns -1.  So, no
    > matter what you do, string.find() will evaluate to "True"


    It will evaluate as false if the substring is found at the beginning
    (position 0).

    > You could use it like this:  if file_str.find("Geometry") != -1:
    >
    > but you probably want to use: if "Geometry" in file_str:


    This is good advice, however.

    John
    John Yeung, Sep 3, 2009
    #4
  5. Helvin

    Tim Chase Guest

    > I have come across this very strange behaviour. Check this code:
    >
    > if file_str.find('Geometry'):


    While the "anser" is to compare the results of .find() with -1,
    but the more Pythonic answer is just to use "in":

    if "Geometry" in file_str:

    which reads a lot more cleanly, IMHO.

    -tkc
    Tim Chase, Sep 3, 2009
    #5
  6. On Thursday 03 September 2009 07:10:37 Helvin wrote:
    > Hi,
    >
    > I have come across this very strange behaviour. Check this code:
    >
    > if file_str.find('Geometry'):
    > #if file_str.endswith('Data_Input_Geometry.txt'):
    > print 'I found geometry'
    > elif file_str.find('Material'):
    > print 'I found material'
    > The amazing thing is when file_str = 'C:\Qt\SimLCM\Default
    > \Data_Input_Material.txt',
    > the first if statement if fulfilled, that seemingly says that in this
    > file_str, python actually finds the word 'Geometry'.
    > I know this, because the line: 'I found geometry' is printed. However,
    > if instead of using file_str.find(), I use file_str.endswith(), it
    > does not exhibit this strange behaviour.
    >
    > Obviously, I want the elif line to be true, instead of the first if
    > statement.
    >
    > Does anyone know why this is happening?
    > Help much appreciated!


    The interactive Interpreter is your friend:

    s = "a;kljghkahklahdfgkjahdfhadafjd;l"
    s.find("banana")
    -1
    bool(_)
    True

    - Hendrik
    Hendrik van Rooyen, Sep 3, 2009
    #6
    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. K B
    Replies:
    2
    Views:
    392
    Chuck
    Mar 21, 2007
  2. Jonathan Shan

    Mystery String

    Jonathan Shan, Jul 25, 2006, in forum: C Programming
    Replies:
    19
    Views:
    602
    Eric Sosman
    Jul 27, 2006
  3. Alan G Isaac
    Replies:
    4
    Views:
    431
    Alan G Isaac
    Sep 13, 2009
  4. Mark Moeykens
    Replies:
    2
    Views:
    526
    Mark Moeykens
    Sep 7, 2010
  5. Uncle Ben

    mystery string code - help!

    Uncle Ben, Apr 20, 2011, in forum: Python
    Replies:
    2
    Views:
    248
    Uncle Ben
    Apr 21, 2011
Loading...

Share This Page