Join the discussion
Ask questions, share your code and help other Python learners. It's free.
Log in to comment, vote and follow discussions.
Continue with Google
or
Already have an account?
New to Space Python?
Check your inbox
We sent an activation link to . Open it in this browser and you'll come straight back here, logged in.
Nothing after a few minutes? Check your spam folder.
Comments 4
Join the discussion CommentAlready have an account? Log in
The difference is what happens when the text isn't there:
-
find()returns -1-
index()raises ValueErrorUse
find()when "not found" is normal,index()when it should never happen and you want to know loudly.So index() is like an assertion. Nice.
Oof, I have exactly that bug somewhere. Off to fix it.
Careful with find() in conditions:
if s.find("x"):is wrong, because position 0 is falsy and -1 is truthy! Useif "x" in s:for simple checks.