Boolean Values and Operators
The in operator is a boolean operator that returns True if a substring is found in a larger string, and False otherwise.
A Small Search with a Boolean Result
The in operator answers a focused question about strings: does one string appear inside another? Its answer is a boolean value. If the substring is found, the result is True. If it is not found, the result is False. This makes in useful both for checking text and for controlling what happens in an if statement.
The in operator is a boolean operator that returns True when a substring is found in a larger string and False when it is not found.
Reading Characters as a Consecutive Match
A substring is not merely a collection of characters that appear somewhere in the larger string. The characters must form one consecutive, unbroken sequence, and they must appear in the exact order specified. For example, the substring cat is found in concatenate because the letters c, a, and t occur together in that order. A search for cta would not match the same text because the order is different.
The essential test is not whether the individual characters occur somewhere. The test is whether the complete substring appears as one uninterrupted sequence in the required order.
From Match to True or False
Evaluating a Substring Check
Determine the result of cat in concatenate.
- 1Identify the substring
The smaller string being searched for is cat.
- 2Inspect the larger string
The larger string is concatenate. It contains the consecutive sequence c, a, t.
- 3Convert the match to a boolean result
Because the complete substring appears in the larger string in the required order, the in expression returns True.
True
What do you think happens?
What do you think is the result of dog in catalog?
Reveal answer
False
The letters d, o, and g do not appear as one consecutive sequence in catalog.
The operator therefore produces a direct boolean expression. A successful substring match becomes True, while an unsuccessful match becomes False. The result can be read on its own or used as the condition of an if statement.
Case Changes the Result
The in operator is case-sensitive. An uppercase letter and a lowercase letter are not treated as the same character for this check. Consequently, A in banana returns False, while a in banana returns True. The lowercase a occurs in banana, but the uppercase A does not.
Consider a check for cat in The Cat. The uppercase C in the larger string does not match the lowercase c in the search string, so the result is False. Changing the larger string to The cat makes the matching letters use the same case, so the result is True.
Using Results to Control Flow
Because in returns True or False, it can serve as the condition in an if statement. The program can take one path when a substring exists and another path when it does not. The important reasoning step is to evaluate the substring check first: determine whether the characters appear consecutively, in order, and with matching case; then use the resulting boolean value to control the decision.
Reasoning Before a Decision
A program checks whether the substring code appears in the text learn code.
- 1Locate the candidate sequence
The text contains the consecutive sequence code.
- 2Determine the boolean value
The substring is present in the required order, so the in expression evaluates to True.
- 3Use the result
An if statement can use this True value to follow its matching branch.
The condition is True, so the presence check succeeds.
Mistakes Beginners Make
Common mistake Ignoring capitalization
What it looks likeChecking cat in The Cat
Why it failsThe search uses lowercase c, while the matching word in the larger string begins with uppercase C. The in operator is case-sensitive.
Do this insteadCompare strings with the intended capitalization, and remember that changing letter case can change the boolean result.
Common mistake Accepting separated characters as a substring
What it looks likeLooking for dog in doing
Why it failsA substring must be a consecutive, unbroken sequence in the exact order. Individual letters appearing in different positions do not create a match.
Do this insteadLook for the complete substring as one continuous sequence.
Common mistake Accepting the right letters in the wrong order
What it looks likeLooking for cta in concatenate
Why it failsThe letters in the larger string do not form cta in that order.
Do this insteadCheck both the character order and the fact that the characters are consecutive.
Practice the Search
Predict whether each expression returns True or False: sun in sunshine; rain in sunshine; A in banana; a in banana; cat in concatenate.
Hints
- Search for the complete substring as one consecutive sequence.
- Check the order of the characters.
- Compare uppercase and lowercase letters exactly.
| Expression | Result |
|---|---|
| sun in sunshine | True |
| rain in sunshine | False |
| A in banana | False |
| a in banana | True |
| cat in concatenate | True |
Answers follow the rules of consecutive order and case-sensitive matching.
What to Remember
- 1The in operator checks whether a substring appears in a larger string.
- 2A successful check returns True, and an unsuccessful check returns False.
- 3The substring must appear as a consecutive, unbroken sequence in the exact order specified.
- 4The check is case-sensitive, so uppercase and lowercase letters can produce different results.
- 5Because the result is boolean, in can be used as the condition of an if statement.
At a glance
- Use in to test whether one string appears inside another string.
- The result is True for a matching consecutive sequence and False otherwise.
- Character order and capitalization both matter.
- The boolean result can control an if statement.