Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > string find mystery

Reply
Thread Tools

string find mystery

 
 
Helvin
Guest
Posts: n/a
 
      09-03-2009
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
 
Reply With Quote
 
 
 
 
Sean DiZazzo
Guest
Posts: n/a
 
      09-03-2009
On Sep 2, 10:10*pm, Helvin <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
John Yeung
Guest
Posts: n/a
 
      09-03-2009
On Sep 3, 1:10*am, Helvin <(E-Mail Removed)> 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
 
Reply With Quote
 
John Yeung
Guest
Posts: n/a
 
      09-03-2009
On Sep 3, 1:45*am, Sean DiZazzo <(E-Mail Removed)> 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
 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      09-03-2009
> 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




 
Reply With Quote
 
Hendrik van Rooyen
Guest
Posts: n/a
 
      09-03-2009
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mystery string code - help! Uncle Ben Python 2 04-21-2011 12:58 AM
Mystery Threads - How do you find out where they come from? Mark Moeykens ASP .Net 2 09-07-2010 06:36 PM
Re: string interpolation mystery in Python 2.6 Alan G Isaac Python 4 09-12-2009 11:26 PM
VS2005 Find function stopped working - OT mystery K B ASP .Net 2 03-21-2007 02:30 PM
Mystery String Jonathan Shan C Programming 19 07-27-2006 02:23 AM



Advertisments