Lecture 14 - Exercises

14A - String Methods

  1. What do each of the following expressions evaluate to?

    1. "Wow".replace("W", "t").upper()
    2. "banana".find("a")
    3. " O_O ".strip().replace("_", "-")
    4. "banana".count("a")
  2. Write a single expression that evaluates to the number of occurrences of the letter ā€œEā€, regardless of case, in a string s.

14B - String Comparisons and Ordering

  1. Evaluate the following expressions:

    1. "To be or not to be".find("be") == 4
    2. "Boo".replace("o", "O").lower() <= "boo"
    3. "no" in "To be or not to be"
    4. "stark" not in "Tony Stark"
    5. "bat" > "rat"
    6. "tap" < "bear"
    7. "Jam" < "bet"
    8. "STEAM" > "STEP!"
  2. What does the following program print?

    def sub_lt(s):
        count = 0
        for i in range(len(s)):
            if s[i:] < s:
                count += 1
        return count
    
    print(sub_lt("brambleFork"))