1
1
Aaron Stevens
4 February 2009
CS108 Lecture 08: Computing with Text
String module operations Character encoding/decoding
2
Overview/Questions How is text represented within computer? How - - PDF document
CS108 Lecture 08: Computing with Text String module operations Character encoding/decoding Aaron Stevens 4 February 2009 1 Overview/Questions How is text represented within computer? How can we manipulate text in our programs? 2 1
1
String module operations Character encoding/decoding
2
3
4
5
6
7
Refer to table 4.2 on page 96 of Zelle for a summary of the Python string module. http://docs.python.org/lib/node42.html shows the complete list.
Returns a copy of <str> in uppercase. upper(<str>) Split <str> into a list of words, using <delim> as delimeter. split(<str>, [<delim]) Remove leading/trailing white space. strip(<str>) Return a copy of <str>replacing all occurrences of <old> with <new>. replace(<str>,<old>,<new>) Meaning Function Returns a copy of <str> in lowercase. lower(<str) Concatenate a list of strings into a single string. join(<list>) Find index of the first occurrence of <sub> in <str>. find(<str>,<sub>) Count occurrences of <sub> in <str>. count(<str>,<sub>) Center string in <width> spaces. center(<str>, <width>) Capitalize entire text or first letter of each word. capitalize(<str>), capwords(<str>)
8
9
10
11
12
13
14
15
16
17
>>> numbers = input("Enter a sequence of ASCII numbers: ") >>> for n in numbers: ... print chr(n),
18
19