0

In core python programming,2nd edition.

6.3 Strings and Operators.

"The membership question asks whether a (sub)string appears in a (nother) string. true is returned if that character appears in the string and False otherwise. Note that the membership operation is not used to determine if a substring is within a string. Such functionality can be accomplished by using the string methods or string module functions find() or index() (and their brethren rfind() and rindex()). "

I'm confused by this.So what dose it mean by saying"Note that the membership operation is not used to determine if a substring is within a string." Shouldn't i use in or not in to find out whether a substring is within a string?

3
  • 1
    Yet another Python book to add to the burn pile... Commented Oct 30, 2015 at 3:03
  • That has to be a typo. The only way that sentence makes sense is if it's supposed to be: "Note that the membership operation is not used to determine where a substring is within a string." since index and find return a location instead of just true/false. Commented Oct 30, 2015 at 3:06
  • @tzaman Thank you for answering my question Commented Oct 30, 2015 at 3:18

1 Answer 1

2

You are correct. Membership/containment can be used to detect presence of a substring within a string. Using it for a single character is simply a degenerate form of substring searching, since a "single character" in Python is a string of length 1.

>>> 'foo' in 'foobar'
True
>>> 'fba' in 'foobar'
False
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you all for answering my question, how do you think about @tzaman's comment, he point out that this maybe a typo.
@Kethylar: Correcting that would make the sentence make sense. The previous two sentences also need to be changed, since the first talks about a substring and the second switches to a character.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.