Calculating sequence length with an accumulator technique (mylen.py)
len("hi there") # built-in string length function
Simple String Processing, Ch 4.2
Strings are sequences of characters
String loop on character sequence (stringLoop.py)
Passing the string as a parameter (stringLoop.py)
Another string loop using range(len(myString)) (stringLoop.py)
Designing the username program p82 (username.py)
Ask for first name (use raw_input)
Ask for last name
Concat together last name plus first letter of first name
Tracing the username program
Design a program to reverse a string. Use the accumulator design pattern.
(reverse.py)
Ask for a string s
Initialize an accumulator rs to an empty string
loop on each character in the string s
Inside the loop concat each character at the beginning of the
reverse string accumulator
Tracing the reverse() program
Design the reverse as a separate function from main() (reverse.py)
main()
Ask for a name
call a reverse function that returns a reversed name
print the reversed name
reverse(s)
initialize an accumulator to an empty string
loop on each character in the string
Inside the loop concat each character at the beginning of the
reverse string accumulator
Designing the month abbreviation program p84
Input: month number (1-12). Output: Month abbreviation, 3 letter.
Make all abbreviations into a long string months = "JanFebMar...Dec"
Note that months[0:3] is Jan, months[3,6] is Feb, etc.
Use the input number to calculate the start index of the abbreviation.
If n is the month then months[3*(n-1):3*(n-1)+3] is the abbreviation.
Print the selected string slice.
Tracing the month abbreviation program
Strings, Lists, and Sequences, Ch 4.3
Strings are sequences of characters.
range[5,100,5] is a sequence of numbers
Lists are sequences of anything
mylist = [1,"Spam", 4, "U"] # note mixing of types is ok!
Notes that we even have a sequence inside a sequence! (The strings).
Slices work on lists just as strings, or any sequence.
mylist[1:3] is ["Spam",4, "U"]
The empty sequence is []
Sequences can be changed! (Unlike strings, by the way)
mylist[2] = "to" # Now look at mylist
Rewrite the months.py program to be simpler (p86)
Make the months variable be a sequence: months = ["Jan","Feb", ... ,"Dec"]
Just index by n-1 now.
Encoding Characters and Strings, Ch 4.4
Strings are sequences characters
Characters are stored in memory words as numbers.
Ascii characters codes are 8 bits, Unicode standards support up to 32
bits for character encoding, allowing for many, many character sets from
many, many languages.
functions ord("a") and chr(33) tranlate between characters and their
numerical representation.
Programming an encoder (encode.py)
Tracing the encoder program
String functions (p96)
string.upper(s), string.lower(s) (pp94-95)
string.split(s), string.join(ls) (pp92-95)
Note the prefix name referring to the string library that is
built in so you don't have to import it.
Read and write the decoder program (p93) using string.split and eval.
input("Enter a number: ") same as eval(raw_input("Enter a number: "))
Use int(x) or float(x) instead of eval(x) (p99)
File Input and Output, Ch 4.6
A simple and efficient way to read lines from a text file (p110 middle)
infile = open("myfile.txt", 'r') # infile is any name you want to chose
for line in infile: # infile has type File (a file variable)
print line
infile.close() # note the dot notation for object related functions
Read and write the userfile.py program p111
Write the myfile.py program to print a file to the console.
Note the double spacing ... why? Because the newline is in the file.
Object oriented notation for calling functions of objects,
infile.close()
Formatting Output using Strings, Ch 4.5.2
Using str(x) to convert a number to a string.
Read and write the dateconvert.py program (p100)
Read and try the string formatting examples p104
Rewrite the convertLoop.py program from lecture 3 to line up columns.
(convertTable.py)