Outline for January 30, 2019

Reading: § 5.1
Due: Homework 2, due on February 1 at 11:55pm


  1. Sequences
    1. Sequences are a series of values in a particular order
    2. In Python predominantly strings and lists but also sets and tuples
  2. Strings
    1. Sequence of characters (characters are strings of length 1)
    2. Strings are immutable; really important for functions
  3. Basic string operations
    1. +, concatenation for strings
    2. *, repetition repeats given value
    3. len() returns length of sequence
    4. s in str returns True if s is a substring of str, False otherwise
  4. Indexing, var[position]
    1. Count from 0 to len(var)−1
    2. Position can be a negative number to count from right
  5. Assignment with indexing doesn’t work as strings immutable
    x = ’hEllo’; x[1] = ’e’ produces an error
  6. Slicing, var[start:end]
    1. Value at index end not included in slice
    2. If omitted, starting value defaults to 0 and ending value defaults to last index + 1
    3. Can use negative index
  7. Looping over strings: for i in str
  8. Example program [strstuff.py]
  9. String methods
    1. Rule: methods that change, add, or delete characters do not alter the string to which they are applied; they return a new string that is a copy of the old string, suitably modified
  10. String methods: type of characters in string (return True or False) [strtype.py]
    1. S.isalpha()True if only alphabetics (letters) in S
    2. S.isalnum()True if only alphanumerics (letters or digits) in S
    3. S.isdigit()True if only digits in S
    4. S.isspace()True if only white space (blanks, tabs, newlines) in S
    5. S.isupper()True if all letters in S are upper case
    6. S.islower()True if all letters in S are lower case
  11. String methods: changing case of letters in string (return result of applying method) [strchcase.py]
    1. S.capitalize() — If the first character of S is a letter, capitalize it
    2. S.title() — Capitalize each word in \str
    3. S.lower() — Change all upper case letters in S to lower case
    4. S.upper() — Change all lower case letters in S to upper case
    5. S.swapcase() — Change all upper case letters in S to lower case and vice versa
  12. String methods: stripping blanks from strings (return result of applying method) [strstrip.py]
    1. S.lstrip() — Delete all leading white spaces from S
    2. S.rstrip() — Delete all trailing white spaces from S
    3. S.strip() — Delete all leading and trailing white spaces from S
  13. String methods: find characters and substrings (return position or cause exception) [strfind.py]
    1. S.find(s) — Return the index of the first occurrence of s in S; −1 if s not in S
    2. S.index(s) — Return the index of the first occurrence of s in S; ValueError exception if s not in S
    3. S.rfind(s) — Return the index of the last occurrence of s in S; −1 if s not in S
    4. S.rindex(s) — Return the index of the last occurrence of s in S; ValueError exception if s not in S
  14. String methods: miscellaneous [strmisc.py]
    1. S.count(s) — Return the number of times s occurs in S
    2. S.startswith(s)True if S starts with s
    3. S.endswith(s)True if S ends with s
    4. S.replace(s,t) — Replace all occurrences of s with t in S

Matt Bishop
Department of Computer Science
University of California at Davis
Davis, CA 95616-8562 USA
Last modified: Version of January 28, 2019 at 8:52PM
Winter Quarter 2019
You can get a PDF version of this