Re: string find mystery

2009-09-03 Thread Hendrik van Rooyen
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('

Re: string find mystery

2009-09-03 Thread 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

Re: string find mystery

2009-09-02 Thread John Yeung
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

Re: string find mystery

2009-09-02 Thread John Yeung
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 fulfill

Re: string find mystery

2009-09-02 Thread Sean DiZazzo
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'): >    

Re: string find mystery

2009-09-02 Thread Stephen Hansen
On Wed, Sep 2, 2009 at 10:33 PM, Helvin Lui wrote: > Thanks! I just realised that too, but I used the condition:.find() > > 0 But I think your's is better. > Simple programming knowledge... > < > Ah, but != 0 vs > 0 isn't a question of better, but correctness: because if .find() returns 0,

Re: string find mystery

2009-09-02 Thread Helvin Lui
Thanks! I just realised that too, but I used the condition:.find() > 0 But I think your's is better. Simple programming knowledge... > < I made a blog post: http://learnwithhelvin.blogspot.com/2009/09/1-is-true-if-loops.html

Re: string find mystery

2009-09-02 Thread Stephen Hansen
> > 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 i

string find mystery

2009-09-02 Thread 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