14 Strings in Python
14 Strings in Python
Jerry Cain
CS 106AX
October 27, 2023
slides leveraged from those constructed by Eric Roberts
Selecting Characters from a String
• A string is (still) an ordered collection of characters. The
character positions in a Python string are, as in most computer
languages, identified by an index beginning at 0.
• For example, if s is initialized as
s = "hello, world"
the characters in s are arranged like this:
h e l l o , w o r l d
0 1 2 3 4 5 6 7 8 9 10 11
• You can select the "w" toward the end of this string using the
expression
s[-5]
"isthay"
"isway"
"igpay"
"atinlay"
line word
result vp head start tail
word i chi ch
"this is pig latin" "latin"
"this"
"pig"
"is"
"isthay
"isthay
"isthay
"isthay
"isthay
isway
"isthay 2"
0
1
"isthay"
isway
isway
""igpay
isway
isway"" " "th"
igpay
igpay" "p" 12
"l"
atinlay" 0 "this"
-1
5
8 0 "atin"
10
11
12
13
14
15
16
17
1
2
3
4
5
6
7
8
9 "is""n"
"ig""h"
"s"
"p"
"g"
"
"l"
"a"
"t"
"i""1
0"t"
2
Implement extract, which lists all key value pairs, one per line,
embedded within the query string of a URL. For example:
extract("https://explorecourses.stanford.edu/search?view=catalog&"\
"academicYear=&page=0&q=CS&filter-departmentcode-CS=on&"\
"filter-coursestatus-Active=on&filter-term-Winter=on")