CS150 - Fall 2012 - Class 5

  • Exercise

  • admin
       - lab tutors Sun-Th 7-9pm in MBH 505 if you need more help

  • recall the basic structure of the for loop we saw last time:

       for i in range(num_times):
          statement1
          statement2
          ...

       the statements will get executed "iteration" times and i will change at each iteration starting at 0 and going to iteration-1

  • what do we know so far about strings?
       - create them
          - using double quotes
          "this is a string"
          
          - using single quotes
          'this is a string'

          - using triple quotes for multiline strings
          """this is
          a string"""
       - concatenate them using '+'
          "this is " + "a string"

       - convert numbers to them
          >>> x = 10
          >>> "My favorite number is " + str(x)

  • What does '*' do for strings?
       >>> "this" * 3
       'thisthisthis'
       >>> "I " + "really, " * 5 + "like ice cream"
       'I really, really, really, really, really, like ice cream'
       >>> print "Please don't make me repeat myself\n" * 5
       Please don't make me repeat myself
       Please don't make me repeat myself
       Please don't make me repeat myself
       Please don't make me repeat myself
       Please don't make me repeat myself

  • loops over strings
       - run print_vertical function in string_basics.py code
          - we can pass it a string
          - and it prints each character on a line by itself
       - how could we do this?
          - we need a way to access individual characters of the string
          - in Python, this is accomplished using the [] to "index" into the string
       
          >>> test = "my string"
          >>> test[1]
          'y'
          >>> test[0]
          'm'
          >>> test[7]
          'n'
          >>> test[10]
          Traceback (most recent call last):
           File "<stdin>", line 1, in <module>
          IndexError: string index out of range

          - notice that like loops, our indexing starts at 0 (NOT 1)
          - Be careful, if you ask for an index that is longer than the string, you will get an error
          - One other useful functions for strings in the len (short for length) function

          >>> len(test)
          9

       - knowing this, how could we write the print_vertical function?
       - look at print_vertical function in string_basics.py code
          - we just iterate over each of the indices in the string and print out each character
          - this will be common way of iterating over strings
       
  • Write a function that reverses a string
       - remember "" represents an empty (or new) string
       - and + for strings concatenates strings
       - look at the reverse function in the string_basics.py code

  • negative indexing
       - Python has a few other nice indexing tricks for strings
       - negative indexing: if you supply a negative index, Python counts from the end of the string

          >>> test = "my string"
          >>> test[-1]
          'g'
          >>> test[-3]
          'i'
          >>> test[-10]
          Traceback (most recent call last):
           File "<stdin>", line 1, in <module>
          IndexError: string index out of range

  • slicing
       - sometimes we want more than just one character (this is called "slicing")
       - Python also allows us to grab some range of characters in a string using the ':' to specify a range

          string[<start_index>:<end_index>]

          - which will give you the characters starting at <start_index> and ending at the character BEFORE <end_index>
          - if <start_index> is not specified, then it starts from the beginning
          - if <end_index> is not specified, then it goes until the end

          >>> test = "my string"
          >>> test[3:8]
          'strin'
          >>> test[3:9]
          'string'
          >>> test[2:4]
          ' s'
          >>> test[:4]
          'my s'
          >>> test[3:]
          'string'

  • look at apples_and_bananas function in string_basics.py code : what does it do?
       - high level: replaces each vowel in the sentence "i like to eat apples and bananas" with the parameter vowel passed in
       - A few details
          - for each of the vowels, we replace them with the vowel parameter that was passed in

  • objects in Python
       - An important concept in programming is objects. An object has data associated with it as well as methods that define additional behavior/functionality for that data type
       - methods vs. functions
          - methods are similar to functions in that they are blocks of code that can have parameters
          - however
             - methods are called with respect to a particular object
                - this is done using the dot notation '.'

                vowels.find(vowel)
                sentence.replace(ch, vowel)

                are both instances of method calls on string objects
             - methods also implicitly have access to the state of the object they are being called on
                 - notice for the above to method calls we do not pass the strings as parameters
                   - for example, sentence.replace(vowels[i], v) says "replace v for vowels[i] IN sentence"
                - the methods have access to data of the object and therefore do not need that information as a parameter