Lecture 8: Strings and Files
Craig Zilles (Computer Science) March 22, 2020 https://go.illinois.edu/cs105sp20
CS 105 Lecture 8: Strings and Files Craig Zilles (Computer Science) - - PowerPoint PPT Presentation
CS 105 Lecture 8: Strings and Files Craig Zilles (Computer Science) https://go.illinois.edu/cs105sp20 March 22, 2020 To Today 1. Slicing 2. Splitting and Joining 3. String Formatting (redux) 4. Files Extensions, Writing, flushing,
Craig Zilles (Computer Science) March 22, 2020 https://go.illinois.edu/cs105sp20
2
counting spaces
my_str = "CS 105" print(my_str[:3]) print(my_str[2:]) print(my_str[1:4])
3
String 6 001000011 001010011
Type Number of characters Characters (Stored using Unicode encoding)
000100000 000110001 000110000 000110101 ‘CS 105’ C S 1 5
my_str = "CS 105" print(my_str[-4:-2]) A) 'S 1' B) 'S 10' C) ' 1' D) ' 10' E) None of these
4
String 6 001000011 001010011
Type Number of characters Characters (Stored using Unicode encoding)
000100000 000110001 000110000 000110101 ‘CS 105’ C S 1 5
def remove_parentheticals(string): while '(' in string:
close_index = string.find(')') string = string[:open_index] + string[close_index + 2:] return string
5
why; that is, what will be the result of the split() confuses me a lot.
exceptions?
6
mylist = input.split(separator) … process mylist …
7
"control")
8
# num digits
# < > ^
# fill with chars
9
10
ls ls -l
dir
cat whole file at once
11
12
Example program 1: Diary that records user input into a file. with open('filename', 'w') as outf: closes file when code block ends
13
14
A) open('filename', 'r') B) open('filename', 'x') C) open('filename', 'i') D) open('filename', 'a') E) open('filename', 'e')
15
Processing a CSV manually:
Example program 2: Create a list of Illinois U.S. representatives
16
17
18
A) for line in lines[:1]: B) for line in lines[1:]: C) for line in lines[:2]: D) for line in lines[2]: E) for line in lines[2:]:
19
newlist = [] for thing in collection: if thing meets criteria: newlist.append(thing)
20