CS150 - Fall 2011 - Class 5
Exercise
admin
- midterm
- 1 midterm instead of 2
- Th. Oct. 20 7:30-9:30pm in MBH 104
- midterms: 20% -> 15%
- final: 20% -> 25%
- lab prep
- remote access
recall the basic structure of the for loop we saw last time:
for i in range(iteration):
statement1
statement2
...
the statements will get executed "iteration" times and i will change at each iteration starting at 0 and going to iteration-1
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]
>>> 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
- the "find" method finds the index of the occurrent of the parameter in the string (-1 if it does not occur)
- to generate the remaining_vowels we "splice" out the one vowel we found leaving everything else. This is a common operation
- for each of the remaining vowels, we replace them with the vowel parameter that was passed in
objects in Python
- ints and floats are just numbers. They have operators that allow us to combine them, etc. but nothing more
- An important concept in programming is objects. Unlike basic data types 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, vowels.find(vowel) says "find vowel IN vowels"
- the methods have access to data of the object and therefore do not need that information as a parameter
What other methods might we want for strings?
- upper / lower
- find
- isalnum
- isalpha
- isdigit
- islower
- replace
- startswith
- capitalize
- Many more, look at the documentation:
http://docs.python.org/library/string.html