Create a week10 subdirectory in your cs21/class directory and copy over my starting point files:
$ cd $ cd cs21/class $ pwd /home/your_user_name/cs21/class $ mkdir week10 $ cd week10 $ pwd /home/your_user_name/cs21/class/week10 $ cp ~newhall/public/cs21/week10/* .After class, you can copy over my files by doing:
$ cp ~newhall/public/cs21/week10/tia* .
$ man asciiUse the arrow keys to navigate, and 'q' to quit.
You should never memorize the ascii encoding, but it is important to know that the encodings for 'a' to 'z' are contiguous, as are 'A' to 'Z' and '0' to '9'. Internallly, python uses the ascii encoding of characters to compare strings. For example, for the following expression:
"ant" == "any"Python first comapares the ascii values the first character in each string (97 for 'a' to 97 for 'a'), since they are equal, python then compares the ascii value of the second character in each string (110 for 'n' to 110 for 'n'), since they are equal, python then compares the ascii value of the third character in each string (116 for 't' to 121 for 'y'), since the ascii values are not equal, python evaluates the expression to False.
The ascii encoding also explains why the following relational expressions evaluate to True:
"Zoo" < "ant" "zoo" > "ant"because the ascii encoding for 'Z' is less than that for 'a' (90 vs. 97) the string "Zoo" is less than the string "ant", and because the ascii value for 'z' is greater than for 'a' (122 vs. 97) the string "zoo" is "Zoo" < "ant" greater than the string "ant".
You can use the ord function to get the ascii value for any character, and the chr function to get the character string for any numeric ascii value:
print ord('a') # prints 97 print chr(99) # prints the string c
Let's try out a few things using what we know about ascii:
$ python ascii.py Enter a phrase: hello there h : 104 e : 101 l : 108 l : 108 o : 111 : 32 t : 116 h : 104 e : 101 r : 114 e : 101
Remember you can create a list of all the characters in the string by calling the list constructor with the string as an argument:
s = "hello" l = list(s) print l ['h', 'e', 'l', 'l', 'o']Since list elements can be modified, you can change elements in the list:
l[2] = 't'And, you can create a string from a list of strings using the join method of the string class:
new_s = "".join(l) print new_s hetlo