Compsci 101, Fall 2012, Lab 2

This lab has three parts. In the first part you'll practice with String slicing operators and loops.

Turn in this page for your group

String Slicing and Dicing

Assume you've typed these four string values and associated variables into your Python interpreter/console window:

a = "computational thinking"
b = "duke university"
c = "python code"


You may want to actually type these into a console window, though you should first try to do the problems below using paper, pencil, discussion, and thinking (no computer)

You'll be given several strings. You should use the slicing operator, string concatenation, string multiplication (by an int, as in "abc" * 3), or indexing to form each of the words below. See the examples for details. Each use of any operator (slicing, indexing, concatenation, and multiplication) has a cost of one. Indicate the cost of your construction.

  1. "honk" formed from c[3:6]+b[2]

    This has a cost of three: one slice, one catenation, one index.

    The value of c[3:6] is hon because the slice starts at the character whose index is 3 which is 'h' and goes up to, but doesn't include the character whose index is 6.

    the value of b[2] is k because it's the single character whose index is 2. Adding or concatenating these strings together makes "honk".

  2. "puking" formed from a[3:5]+a[18:], again the cost is three: two slices and one catenation with +.

    Note that the value of a[18:] starts at the index-18 character and goes until the end of the string, i.e., is king. You can also multiply strings by a number at a cost of 1, i.e., a[3:5]*2 is "pupu" at a cost of 2: one slice and one multiply.

  3. Create "docking" and provide its cost.

  4. Create "cocoa" and provide its cost.

  5. Create "thought" and provide its cost.

  6. Create "ratatatat" and provide its cost.

  7. Create "duke is cool" and provide its cost.

  8. Create "diversity nation" and provide its cost.

  9. Create "money honey" and provide it's cost.


String and List Vocabulary

A method is a function invoked on an object. For example, the table below shows some string methods.

string methodpurpose
s.upper() returns string upper case version of string s
s.count(sub) returns int number of (non-overlapping) occurrences of sub in s
s.endswith(sub) returns boolean depending on whether s ends with sub
s.find(sub) returns int: first index at which sub occurs in s or -1 if no occurrences
s.split() returns list of s split on whitespace
s.split(sep) returns list of s split on sep, a delimiter
s.strip() returns copy of s withOUT leading and trailing whitespace

list method purpose
lst.count(elt) returns number of occurrences of elt in lst
lst.index(elt) returns first/least index at which elt occurs in lst, generates error if elt not in lst
lst.append(elt) append elt to end of lst, None returned

Functions are applied to objects. For example, len returns an int: the length of a string or list or other sequence/iterable. So len("apple") is 5 and len([1,2,3]) is 3.

Other functions for lists include: sum (returns total, an int, of elements in a list); max,min which return largest and smallest elements in a list; sorted which returns a sorted version of a list; reversed which returns a reversed version of a list.

For example:

function call result returned type returned
len([1,2,3,"apple"]) 4 int
max([5,4,1,2,9,3]) returns 9 same as list elements
max("ape", "bee", "zebra", "wildebeast"] "zebra" same as list elements
min([5,4,1,2,9,3]) returns 1 same as list elements
sorted([5,4,1,2,7]) [1,2,4,5,7] list
reversed([1,2,3,6]) [6,3,2,1] list

Vocabulary Questions

Answer these questions on the handin sheet.
  1. What do you think the name of the string method is that returns the lower case version of a string? (see the String methods above and generalize).

  2. The function max returns the maximal value of a string as well as a list. What is max("science") and why?

  3. What is the value of reversed(sorted([5,4,1,2,8])) and why?

  4. What is the value of sorted(lst).index(min(lst)) for any list lst? Why? What's the value if min is replaced by max?

  5. Why is the value of str.endswith(str[-3:]) True?

  6. When is the value of st.upper().endswith(st) True? (Provide a specific example of when it's true and try to generalize)

  7. The in operator determines if its left operand occurs in its right operand and returns a boolean value. This means 'a' in 'stranger' evaluates to True. What is the value of 5 in [1,2,3,4]?

  8. Explain when the value of st[0:2] in st is False? (Provide a specific example of when it's true and try to generalize)

  9. Explain when the value of st[0:2]*2 in st is True? (Provide a specific example of when it's true and try to generalize)

  10. When is the value of lst.count(lst[0]) the same as the value of len(lst) for a list lst? (Provide a specific example of when it's true and try to generalize)


APT Acronym

Read the Acronym APT statement. Then with your group come up with a general plan for how to form the acronym of the parameter phrase --- your plan should be in English, not in code.

Answer these questions on the handin sheet.

Turn in this page for your group

  1. What string method returns a list that is the words represented by the string, e.g., as separated by spaces? Write code to create a list variable that is the words in parameter phrase using this method. Name the list variable words.

  2. What is a for loop that will loop over each element of the list of words in phrase (use variable words from above) -- don't write the loop body yet, just the loop.

  3. Use the loop, explain how to index each word in the body of the loop to access the first character of the word, i.e., what's the Python code to do that and append the first character to the end of a string as shown below in the blank
         acro = ""
         for loop-you-wrote-above:
             acro = acro + ______________
         return acro