1
h06
CS8 M19-B
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu section
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone"

h06: Perkovic 4.1 (Strings, Revisited), 4.2 (Formatted Output)

ready? assigned due points
true Tue 08/20 02:00PM Tue 08/27 02:00PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the lowest scores (if you have zeros, those are the lowest scores.)


READING ASSIGNMENT

Please read Perkovic 4.1 (Strings, Revisited), 4.2 (Formatted Output). Then complete these problems and turn in your completed homework in lecture on the due date.

  1. (10 pts) Please fill in the information at the top of this homework sheet, including your name and umail address. Put the time your discussion section starts () in the space indicated (the one you are registered for—even if you usually attend a different one.) If the other two items apply, please fill them in as well. Please do this every single time you submit homework for this class.
  2. Section 4.1 discusses indexing and slicing of strings, as well as some methods (functions) that can be invoked on strings.

    Assume that the following assignment statements have been executed.

    fname="Chris"
    lname="Gaucho"
    schools=["Cal Poly","UCSB","UCSD","MIT"]
    

    What will each of the expressions below evaluate to?

    Points Expression Result Points Expression Result
    (5 pts) fname[0]   (5 pts) lname[:2]  
    (5 pts) lname[0:2]   (5 pts) lname[-3:]  
    (5 pts) schools[0]   (5 pts) schools[1:]  
    (5 pts) schools[0:2]   (5 pts) fname.find('h')  
    (5 pts) schools[0][0]   (5 pts) lname.find('ch')  
    (5 pts) schools[0][0:2]   (5 pts) lname.replace('cho','di')  
  3. (5 pts) In Python, how do we define a string that consists of more than one line of text?

  4. (5 pts) For the Python code in the left box, write the output in the right box

    the_list = ["8","16","24","32"]
    for course in the_list:
       print(course, end="|")
    
  5. When you import the module time, the function time.time() gives “seconds since the epoch”, while time.gmtime gives a representation of the time in UTC.

    1. (5 pts) What is “the epoch”?

    2. (5 pts) What is UTC?

    3. (5 pts) If you want the local time instead of UTC, what function of the time module do you call instead of time.gmtime()?

  6. (5 pts) As shown on p. 106, if you enter import time at the >>> prompt, then enter the following:

    >>> time.strftime('%A %b/%d/%y %I:%M %p', time.localtime())
    

    the resulting output is the current date and time in the format:

    Sunday Aug/18/19 02:11 PM
    

    Using the table of values on p. 107 as a reference, how would you change the call to strftime above to get the date in this format (note, the output may slightly differ depending on when you work on the homework):

    August 18, 2019