string find mystery

Helvin helvinlui at
Thu Sep 3 07:10:37 CEST 2009


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

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

Very puzzled,

More information about the Python-list mailing list