473,289 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

shouldn't 'string'.find('ugh') return 0, not -1 ?

the subject pretty much says it all.
if I check a string for for a substring, and this substring isn't found,
should't the .find method return 0 rather than -1?
this breaks the

if check.find('something'):
do(somethingElse)

idiom, which is a bit of a pity I think.

cheers,

-jelle

Oct 31 '07 #1
4 1975
On Wed, 31 Oct 2007 13:31:06 +0000, jelle wrote:
if I check a string for for a substring, and this substring isn't found,
should't the .find method return 0 rather than -1?
this breaks the

if check.find('something'):
do(somethingElse)

idiom, which is a bit of a pity I think.
And what should ``'string'.find('str')`` return? How do you distinguish
the not found at all case from the found at the very beginning case!?

The simple test you want can be written this way:

if 'something' in check:
do(something_else)

Ciao,
Marc 'BlackJack' Rintsch
Oct 31 '07 #2
jelle <je**********@gmail.comwrites:
the subject pretty much says it all.
if I check a string for for a substring, and this substring isn't found,
should't the .find method return 0 rather than -1?
How would you treat the case of 'something' being at the beginning of
the string? After all, find returns the index.
this breaks the

if check.find('something'):
do(somethingElse)

idiom, which is a bit of a pity I think.
if 'something' in check:
do(somethingElse)
Oct 31 '07 #3
On Oct 31, 9:31 am, jelle <jelleferi...@gmail.comwrote:
the subject pretty much says it all.
if I check a string for for a substring, and this substring isn't found,
should't the .find method return 0 rather than -1?
this breaks the

if check.find('something'):
do(somethingElse)

idiom, which is a bit of a pity I think.
string.find has always been kind of a wart in Python; that's why
they're getting rid of it. For testing for the presence of a
substring, use the in operator:

if "something" in check:
do(somethingElse)

When you need the position of a substring, using the index methods:

i = check.index("something")

index returns an exception of the substring is not there, so no need
to worry about what it returns.

Finally, it really won't kill you to do this:

if check.find("something") >= 0:
do(somethingElse)
Carl Banks
Oct 31 '07 #4
On Oct 31, 8:11 am, Carl Banks <pavlovevide...@gmail.comwrote:
>
string.find has always been kind of a wart in Python; that's why
they're getting rid of it. For testing for the presence of a
substring, use the in operator:

Per the Python 3000 presentation given by Guido Van Rossum at
PyCon February 2007, the new Bytes type will have a "wart".

"Has some string-like methods, e.g. .find()"
Oct 31 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jesse | last post by:
I have a string with the following information. $myString = "This is my example string please. Please visit http://www.mylink.com . More example text goes here. another link http://www.link2.com...
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
11
by: Martin Jørgensen | last post by:
Hi, - - - - - - - - - - - - - - - #include <iostream> #include <string> #include <map> using namespace std; int main() {
6
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time...
7
by: bcpkh | last post by:
Hello All I need to check a string to make sure it does not contain any non numeric characters, the problem that I face is that the string is fairly long, 2784601121574585949, strtol etc. can't...
5
by: surapong | last post by:
Is there any easy way to extract the string from enum variable?? e.g. I have enum TheEnum {ONE, TWO, THREE}; I would like to generate the array of string containing {"ONE", "TWO", "THREE"}
4
by: pankajprakash | last post by:
hi, i have a string which is return by the sqlserver 2005, which contains a "-" (separator), now i need to split this string, but i occurs error. string a ; // a = "Code - N/A" ...
5
by: gargonx | last post by:
Say i have the two methods: def ReturnMethod(request, x): if request is True: return x else: print "No String for you...False!" def SendMethod(request): xstring = "Some text"...
5
by: Anders Borum | last post by:
Hi! While implementing a property manager (that supports key / value pairs), I was wondering how to constrain T to a struct or string type. Basically, I guess what I'm looking for is the common...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.