string find mystery

Stephen Hansen apt.shansen at gmail.com
Thu Sep 3 01:19:17 EDT 2009


>
> 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.
>
>
The problem is str.find returns -1 on failure; and -1 is a true value. Only
0, empty string, empty sequences, etc, are false values.

So, you have to test, 'if find_str.find(pattern) != -1:'

HTH,

--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090902/4f0b14ce/attachment-0001.html>


More information about the Python-list mailing list