string find mystery

H

Helvin

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
 
S

Sean DiZazzo

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
 
J

John Yeung

        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
 
J

John Yeung

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
 
T

Tim Chase

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
 
H

Hendrik van Rooyen

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top