Concepts / Conditional Statements with if and else

Conditional Statements with if and else

The in operator is a boolean operator that returns True if a substring is found in a larger string, and False otherwise.

  • Programming

A Question About Text

A program often needs to ask whether some text appears inside a larger piece of text. For example, it may need to check whether a message contains a particular word. Python's in operator answers this question with a boolean result: True when the substring is found and False when it is not.

The in operator checks for a substring inside a larger string. It returns either True or False.

Matching a Consecutive Sequence

For an in expression to return True, the smaller string must appear as a consecutive, unbroken sequence of characters in the larger string. The characters must also appear in the exact order specified. A search for cat succeeds in copycat because the three characters c, a, and t appear together in that order. A search does not succeed merely because the larger string contains those characters somewhere separately.

matchesmatchesmatchescsubstringclarger stringasubstringolarger stringtsubstringplarger stringylarger stringclarger stringalarger stringtlarger string
Where does the smaller string appear as one unbroken sequence inside the larger string?

Checking for cat

Determine the result of cat in copycat.

Identify the substring: The substring being searched for is cat.

Inspect the larger string: The larger string is copycat.

Check the sequence: The consecutive characters c, a, and t appear together and in that order inside copycat.

The expression returns True.

Boolean Results

The in operator is a boolean operator. A boolean expression using in evaluates to True when the substring is found in the larger string and False when the substring is not found.

ExpressionResultReason
cat in copycatTruecat appears as a consecutive sequence
dog in copycatFalsedog does not appear
copy in copycatTruecopy appears as a consecutive sequence
cap in copycatFalsethe characters are not the required consecutive sequence

Generated examples showing how substring presence determines the boolean result.

What do you think happens?

What result does cat in educational return?

  • True
  • False
Reveal answer

Answer: False

The characters c, a, and t do not appear together in that exact order in educational.

Case-Sensitive Searches

The in operator is case-sensitive. Uppercase and lowercase letters are treated as different characters for the search. Therefore, searching for an uppercase A is different from searching for a lowercase a.

searchescontains lowercase asearchesdoes not contain uppercase AasearchAsearchbananalarger stringbananalarger stringTrueresultFalseresult
Why do searches for A and a produce different results in the same string?

Comparing letter case

Determine the results of a in banana and A in banana.

Search for lowercase a: The larger string banana contains lowercase a, so this search succeeds.

Search for uppercase A: The larger string banana does not contain uppercase A. The lowercase a characters do not count as uppercase A.

a in banana returns True, while A in banana returns False.

Branching on the Result

Because an in expression produces True or False, it can be used in an if statement. The if branch runs when the in expression returns True. When the expression returns False, an else branch can provide the alternative path. In this way, substring presence controls program flow.

TrueFalsecat in messageboolean expressionif branchTrueelse branchFalse
How does the boolean result of an in expression determine which if or else branch runs?

Imagine checking whether a text message contains the substring urgent. An if statement can choose one path when urgent is present and the else statement can choose another path when it is absent. The important sequence is: search for the substring, receive True or False, then follow the matching branch.

Mistakes to Avoid

  • Ignoring case sensitivity

    The in operator distinguishes uppercase A from lowercase a.

    Fix: Compare the exact capitalization used in the substring with the capitalization in the larger string.

  • Assuming separate characters are enough

    The substring must be a consecutive, unbroken sequence in the exact order.

    Fix: Look for the complete substring as one continuous sequence.

  • Reversing the search relationship

    The question is whether the substring appears inside the larger string.

    Fix: Identify which string is being searched for and which string contains the possible match.

Before evaluating an in expression, name the two roles: the substring being searched for and the larger string being searched. Then check the characters in order and with exact capitalization.

Guided Practice

EASY

Predict the boolean result for each expression. Explain your prediction by identifying whether the substring appears as a consecutive sequence with the exact capitalization shown: sun in sunny, blue in blueberry, cat in concatenate, and A in banana.

Hints
  • First identify the substring and the larger string.
  • Look for consecutive characters in the exact order.
  • Treat uppercase and lowercase letters as different.
  • For sun in sunny, check whether sun appears at the beginning of sunny.
  • For blue in blueberry, check whether blue appears as one unbroken sequence.
  • For cat in concatenate, check both the character order and whether the sequence is continuous.
  • For A in banana, check capitalization as well as presence.

Key Takeaways

  1. The in operator checks whether a substring appears in a larger string.
  2. It returns True when the substring is found and False otherwise.
  3. A match requires consecutive characters in the exact order specified.
  4. The search is case-sensitive, so uppercase and lowercase letters are different.
  5. An in expression can control whether an if branch or an else branch runs.

Key Takeaways

  • Use in to test whether one string appears inside another.
  • The result is always True or False.
  • The substring must occur consecutively and in the exact requested order.
  • The operator is case-sensitive.
  • An if statement can use the result to select a branch, with else handling the False case.