String Slicing

s = 'PythonTime'

How would you slice into this string to obtain the following results?

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').


String Function Warmup

Implement the following functions:

  1. 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?

  2. 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'.

  3. pre_colon(s): If the string parameter contains a colon (':'), return the substring leading to but not including the first ocurrence of this character. For example, calling pre_colon('hello:world') would return the string 'hello'.

    As an extension, write a version of this function pre_colon_ext(s, before) that takes in a boolean parameter before. If this parameter is True, return the substring up to the first colon. If this parameter is False, return the substring after the first colon.

    • pre_colon_ext('hello:world', True) would return 'hello'.
    • pre_colon_ext('hello:world', False) would return 'world'.

  4. at_word(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)]

Test your solutions here .



Lab style problem: CoHome Delivery

(with our apologies to Stanford R&DE)

Brahm is making an app that delivers food from CoHo to students so that they don't have to leave the comfort of their dorm room. He is hiring Stanford students to pick up the food and deliver it. When a student orders food, they get matched with a delivery person, and communicate with them using the app's built in chat platform. Brahm pays the delivery people per order, but keeps some of the profit for himself.

Early on, he finds out that some of the delivery people are sending their phone numbers or email addresses to students through his app chat platform and offering a lower price in order to keep the entire profit. He needs to find a way to detect when delivery people are trying to send their phone numbers or email addresses. Unfortunately, he's too busy to write this program right now, so he wants you to help implement a function that can alert him when someone is trying to circumvent the system.

Specifically, your job is to implement the following function:

def is_message_valid(filename)

which takes as a parameter a filename string corresponding to a text file containing a single message sent by a delivery person. If the message contains a phone number or email address, it is considered invalid and the function should return False. If it contains neither a phone number or email address, the message is valid and you should return True. For the sake of this problem, you can assume that any sequence of 10 consecutive digits is a phone number and an email address is any sequence of characters including the '@' character and at least one character before and after it. You can make the following assumptions:

  1. Any email address or phone number will appear entirely on one line of the message, rather than being broken over multiple lines
  2. If an '@'' character is present on a line, it will not be at the very beginning or end of the line.

This is a lab-style problem. To get started, download the starter PyCharm project here. You will first spend some time discussing various approaches to decomposing a solution to this problem, and will then implement and test it on your laptops. On the digital version of this handout, we've linked a PyCharm project you can download with some starter code for this problem. In section, you will spend some time discussing approaches to these problems, and then will write and test your code in PyCharm.