Section 3. Strings and Lists

Written by Juliette Woodrow, Anna Mistele, John Dalloul, Parth Sarin, and Zheng Lian

PyCharm Project

String Slicing

s = 'PythonTime'

How would you slice the string to receive the following results?

  • 'ython'
  • 'Py'
  • 'Tim'
  • 'Time'
  • 'T'
  • 'PythonTime'
  • 'pythontime'
  • 'e'
  • 'ime'

Remember, strings in Python are 0-indexed. In addition, the slice s[1:8] is inclusive of the first index, and exculsive of the second (that is, it will get the string beginning at index 1 and up to, but not including, index 8, i.e. 'ythonTi'). You can use negative indices to get letters from the back of the string. For example, the slice s[-2:] starts at the second to last character of the string and goes to the end of string. It would be 'me'.

String Searching

Implement the following functions:

  • defront(s): If the string parameter has more than 1 character, return it without its first two characters. Otherwise, return the original string. What would we expect this function to return if the string is exactly 2 characters long?

  • x_end(s): If the string parameter contains the 'x' character, return the string, from the first 'x' to the end of the string. Otherwise, return an empty string. For example, calling x_end('excited') would return the string 'xcited'.

  • is_valid_password(s): Returns True if the string s meets the following criteria, and False otherwise. Password Criteria:
    • Contains the special characters ! and &, in that order (but characters may occur before, in between, and after them)
    • Contains explicitly more characters before the ! than after the &

    Yes, these criteria are as arbitrary and annoying as actual password requirements

  • at_words(s): If the string parameter contains 2 or more '@' characters, return the substring between the first two such characters. Otherwise, return the empty string. For example, calling at_word('xx\@hello\@xx') returns the string 'hello'.

    You might find the 2-parameter version of the s.find(target, start) function useful. This function returns the index in s of the first instance of the string target, searching the range s[start: len(s)].

String Construction

Implement the following functions:

  • make_gerund(s): which adds 'ing' to the end of the given string s and returns this new word. If s already ends with 'ing', add an 'ly' to the end of s instead. You may assume that s is at least 3 characters long.
  • put_in_middle(outer, inner): which returns a string where inner has been inserted into the middle of the string outer. To find the middle of a string, take the length of the string and divide it by 2 using integer division. The first half of the string should be all characters leading up to, but not including, the character at this index. The second half should start with the character at this index and include the rest of the characters in the string.

Word Puzzle

Stacatto Words

We say that a word is a stacatto word if all of the letters in even positions are vowels (i.e., the second, fourth, sixth, etc. letters are vowels). For this problem, the vowels are A, E, I, O, U, and Y. For example, AUTOMATIC, CAFETERIA, HESITATE, LEGITIMATE, and POPULATE are stacatto words. Write a function is_stacatto(word) that returns True if a word is a stacatto word and False otherwise. For this problem, you can assume that word will be a string containing uppercase alphabetic characters only.

Fun with List

Implement the following functions:

  • append_evens(n): Given some non-negative integer parameter, create a list containing all even numbers between 0 and n (inclusive) in descending order.

  • all_substrings(s): Given some string parameter, generate a list containing all of its non-empty substrings. For example, calling all_substrings('car') would return the list ['c', 'ca', 'car', 'a', 'ar, 'r'] (not necessarily in that order). Duplicates should be included.