Lecture 13 - Exercises

13A - Strings and Tuples are Sequences

  1. Consider the following function:

    The result of calling this function with a particular string a is C C S S C C E E Y Y E E. What was a?

  2. What does the following program print?

13B - Indexing and Slicing

  1. Suppose s = "Winter is coming." Which of the following evaluates to "r"?

    1. s[5]
    2. s(5)
    3. s[6]
    4. s(6)
  2. Suppose last_name = "Wehrwein". Which of the following evaluates to "in"? List all that apply.

    1. last_name[7:8]
    2. last_name[-2:8]
    3. last_name[6:-1]
    4. last_name[-3:]
    5. last_name[6:]
  3. Which of the following evaluates to the last character of a string s?

    1. s[len(s) - 1]
    2. s[len(s)]
    3. s[len(s) + 1]
    4. s[42]
  4. Suppose last_name = "wehrwein". For which of the following a and b will last_name[a] == last_name[b] evaluate to True? List all that apply.

    1. a = 1, b = 5
    2. a = 1, b = 7
    3. a = 8, b = -4
    4. a = -2, b = -6
  5. What does the following code print?

  6. Consider the following function:

    Suppose a contains an integer. Which of the following are possible return values of the following call:

    List all that apply:

    1. mit one
    2. nomite
    3. t onime
    4. on time
    5. emit on
    6. timeno time

Problems

  1. Implement the following function:

  2. Implement the following function, which differs only in that it returns the string without vowels:

  3. Implement the following function:

  4. Implement the following function:

  5. Write a function that takes a string and prints all prefixes of the string, including the string itself.

  6. Implement the following function:

  7. Write an add_banner function that takes a string argument and prints that string, surrounded by a rectangle of # symbols. If the string is longer than 80 characters, break the string into multiple lines. You can assume the string does not contain any newlines. An example might look something like the following:

    >>> print_banner("Modify your print_banner to implement text wrapping so that a string longer than 80 characters will be broken into multiple lines.")
    ##################################################################################    
    #Modify your print_banner to implement text wrapping so that a string longer than#
    #80 characters will be broken into multiple lines.                               #
    ##################################################################################
    >>>