WEEK02: numbers and strings
 F: string and list indexing, slicing, and len() function

REVIEW...functions we've learned so far:


  and today we will learn len()


How do computers know when a word is mispelled in a document?
And how do they come up with a list of words the user might have meant?

If I type this: Welcime to Computre Sciennce at Swarthmore Colege!

What algorithms can you think of to find the correctly-spelled words?

For the word "Sciennce", we can all see there's one extra "n" in there.
So one algorithm might be: 

  - delete each letter, one at a time
  - look up each newly-formed word (without the deleted letter) to see if it's a valid word

Let's just focus on the first part: form new words by deleting one letter.
Here's what happens to "Sciennce":

 delete 0th letter:  ciennce  new word: ciennce
 delete 1st letter: S iennce  new word: Siennce
 delete 2nd letter: Sc ennce  new word: Scennce
 delete 3rd letter: Sci nnce  new word: Scinnce
 delete 4th letter: Scie nce  new word: Science   <-- valid word!
 delete 5th letter: Scien ce  new word: Science   <-- valid word!
 delete 6th letter: Scienn e  new word: Scienne
 delete 7th letter: Sciennc   new word: Sciennc


SLICING allows us to select a range of items from a list or string.
Here's a string example:

>>> word = "Sciennce"
>>> word[0:4]
>>> word[5:8]
>>> newword = word[0:4] + word[5:8]
>>> print newword

So the above selects all but the 4th letter!


Formally, slicing uses a [start:stop:step] format (similar to range()).
If you leave off the start, it defaults to 0.
If you leave off the step, it defaults to 1.

Here are some list examples (works same for strings):

>>> L = range(20)
>>> print L
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> L[4:10]
[4, 5, 6, 7, 8, 9]
>>> L[:10]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> L[10:]
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> L[2:20:3]
[2, 5, 8, 11, 14, 17]

STRINGS and LISTS are sequences!!

  s = "hello"
  mystr = "this is fun!"

  mylist = ["dog", "cat", "pony", "fish"]
  nlist = range(10)

both lists and strings are sequences, so they can be used in for loops!

  for c in "hello":
    print c

  for word in ["dog", "cat", "pony", "fish"]:
    print word

you can also access elements of a string or a list with an index:

  print mystr[0]

  print mylist[1]

the len() function gives you the length of a string or list:

>>> mylist = ["dog", "cat", "pony", "fish"]
>>> mystr = "this is fun!"
>>> len(mystr)
>>> len(mylist)

try to write these programs:

$ python its.py 
first name: jeff
 last name: hollinsworth
your ITS username = jhollin1

$ python splitphrase.py 

  This program does some cool string processing.
  Please enter a phrase below.

phrase: computer science is really fun!!

computer science
                 is really fun!!