SLIDE 1
Strings ¡II ¡
SLIDE 2 Review ¡
- Strings ¡are ¡stored ¡character ¡by ¡character. ¡
- Can ¡access ¡each ¡character ¡individually ¡by ¡
using ¡an ¡index: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0 ¡ ¡ ¡ ¡ ¡ ¡1 ¡ ¡ ¡ ¡ ¡ ¡2 ¡ ¡ ¡ ¡ ¡ ¡ ¡3 ¡ ¡ ¡ ¡ ¡ ¡4 ¡ ¡ ¡ ¡ ¡ ¡5 ¡ ¡ ¡ ¡ ¡ ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡7 ¡
"C" ¡ "o" ¡ "m" ¡ "p" ¡ "u" ¡ "t" ¡ "e" ¡ "r" ¡
SLIDE 3 New ¡
- NegaDve ¡indexing ¡can ¡be ¡used. ¡ ¡(Par%cularly ¡
useful ¡for ¡ge0ng ¡characters ¡near ¡the ¡end ¡of ¡a ¡ string.) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0 ¡ ¡ ¡ ¡ ¡ ¡1 ¡ ¡ ¡ ¡ ¡ ¡2 ¡ ¡ ¡ ¡ ¡ ¡ ¡3 ¡ ¡ ¡ ¡ ¡ ¡4 ¡ ¡ ¡ ¡ ¡ ¡5 ¡ ¡ ¡ ¡ ¡ ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡7 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡-‑8 ¡ ¡ ¡ ¡ ¡-‑7 ¡ ¡ ¡ ¡ ¡-‑6 ¡ ¡ ¡ ¡ ¡-‑5 ¡ ¡ ¡ ¡ ¡-‑4 ¡ ¡ ¡ ¡ ¡-‑3 ¡ ¡ ¡ ¡ ¡-‑2 ¡ ¡ ¡ ¡-‑1 ¡
"C" ¡ "o" ¡ "m" ¡ "p" ¡ "u" ¡ "t" ¡ "e" ¡ "r" ¡
SLIDE 4 The ¡basic ¡string ¡for ¡loop ¡
- Use ¡this ¡whenever ¡you ¡need ¡to ¡process ¡a ¡
string ¡one ¡character ¡at ¡a ¡Dme. ¡ ¡
# assume s is a string variable for pos in range(0, len(s)): # do something with s[pos]
SLIDE 5
s = "banana" total = 0 for pos in range(0, len(s)): if s[pos] == "a":
total = total + 1 0 1 2 3 4 5
¡
"b" ¡ "a" ¡ "n" ¡ "a" ¡ "n" ¡ "a" ¡
SLIDE 6
s = "banana" total = 0 for pos in range(0, len(s)): if s[pos] == "a":
total = total + 1 0 1 2 3 4 5
¡
"b" ¡ "a" ¡ "n" ¡ "a" ¡ "n" ¡ "a" ¡
1st ¡itera1on ¡ pos: ¡0 ¡ s[pos]: ¡"b" ¡ total: ¡0 ¡
pos ¡ s[pos] ¡
SLIDE 7
s = "banana" total = 0 for pos in range(0, len(s)): if s[pos] == "a":
total = total + 1 0 1 2 3 4 5
¡
"b" ¡ "a" ¡ "n" ¡ "a" ¡ "n" ¡ "a" ¡
2nd ¡itera1on ¡ pos: ¡1 ¡ s[pos]: ¡"a" ¡ total: ¡1 ¡
pos ¡ s[pos] ¡
SLIDE 8
s = "banana" total = 0 for pos in range(0, len(s)): if s[pos] == "a":
total = total + 1 0 1 2 3 4 5
¡
"b" ¡ "a" ¡ "n" ¡ "a" ¡ "n" ¡ "a" ¡
3rd ¡itera1on ¡ pos: ¡2 ¡ s[pos]: ¡"n" ¡ total: ¡1 ¡
pos ¡ s[pos] ¡
SLIDE 9
s = "banana" total = 0 for pos in range(0, len(s)): if s[pos] == "a":
total = total + 1 0 1 2 3 4 5
¡
"b" ¡ "a" ¡ "n" ¡ "a" ¡ "n" ¡ "a" ¡
4th ¡itera1on ¡ pos: ¡3 ¡ s[pos]: ¡"a" ¡ total: ¡2 ¡
pos ¡ s[pos] ¡
SLIDE 10
s = "banana" total = 0 for pos in range(0, len(s)): if s[pos] == "a":
total = total + 1 0 1 2 3 4 5
¡
"b" ¡ "a" ¡ "n" ¡ "a" ¡ "n" ¡ "a" ¡
5th ¡itera1on ¡ pos: ¡4 ¡ s[pos]: ¡"n" ¡ total: ¡2 ¡
pos ¡ s[pos] ¡
SLIDE 11
s = "banana" total = 0 for pos in range(0, len(s)): if s[pos] == "a":
total = total + 1 0 1 2 3 4 5
¡
"b" ¡ "a" ¡ "n" ¡ "a" ¡ "n" ¡ "a" ¡
6th ¡itera1on ¡ pos: ¡5 ¡ s[pos]: ¡"a" ¡ total: ¡3 ¡
pos ¡ s[pos] ¡
SLIDE 12 Algorithm ¡-‑> ¡FuncDon ¡
- CounDng ¡the ¡number ¡of ¡a ¡certain ¡character ¡in ¡
a ¡string ¡seems ¡like ¡a ¡good ¡candidate ¡for ¡a ¡
def count_a(s): total = 0 for pos in range(0, len(s)): if s[pos] == "a" total = total + 1 return total
¡
SLIDE 13
def count_a(s): total = 0 for pos in range(0, len(s)): if s[pos] == "a": total = total + 1 return total def main(): name = input("What is your name? ") freq = count_a(name) print("Your name has", freq, "A's in it.")
¡
SLIDE 14
- Step ¡1: ¡Change ¡the ¡count ¡funcDon ¡so ¡it ¡takes ¡a ¡
second ¡argument ¡called ¡letter. ¡ ¡The ¡funcDon ¡ should ¡count ¡the ¡number ¡of ¡Dmes ¡that ¡letter
- ccurs ¡in ¡the ¡string ¡(instead ¡of ¡only ¡lowercase ¡a's). ¡
- Step ¡2: ¡Change ¡the ¡main ¡funcDon ¡so ¡that ¡the ¡user ¡
can ¡type ¡in ¡their ¡name ¡and ¡a ¡leVer ¡and ¡the ¡ program ¡prints ¡the ¡frequency ¡of ¡that ¡leVer ¡in ¡their ¡
- name. ¡
- Challenge: ¡Write ¡a ¡funcDon ¡count_dups ¡that ¡
counts ¡(and ¡returns) ¡all ¡occurrences ¡of ¡ consecuDve ¡duplicated ¡leVers ¡in ¡a ¡string. ¡
– e.g., ¡count_dups("balloon") ¡returns ¡2. ¡
SLIDE 15
Not ¡all ¡string ¡problems ¡are ¡solved ¡with ¡for ¡loops. ¡ def get_initial(firstname): first_init = firstame[0] return first_init
SLIDE 16 String ¡ConcatenaDon ¡
- Combines ¡two ¡strings ¡into ¡a ¡new, ¡longer ¡string. ¡
- Uses ¡the ¡same ¡plus ¡sign ¡as ¡addiDon. ¡
s1 = "CS141" s2 = "rocks!" bigstring = s1 + s2 print(bigstring) # prints CS141rocks!
SLIDE 17 String ¡ConcatenaDon ¡
- Unlike ¡print(), ¡string ¡concatenaDon ¡does ¡not ¡put ¡
spaces ¡between ¡your ¡strings. ¡ s1 = "CS141" s2 = "rocks!" bigstring = s1 + " " + s2 print(bigstring) # prints CS141 rocks!
SLIDE 18 Sample ¡problem ¡
- All ¡professor ¡email ¡addresses ¡at ¡Rhodes ¡are ¡
constructed ¡from ¡the ¡professor's ¡last ¡name, ¡ followed ¡by ¡the ¡iniDal ¡leVer ¡of ¡their ¡first ¡
- name. ¡
- We ¡want ¡to ¡design ¡a ¡funcDon ¡that ¡takes ¡a ¡
prof's ¡first ¡and ¡last ¡name ¡and ¡returns ¡their ¡ email ¡address. ¡
SLIDE 19
def make_prof_email(first, last): init = first[0] address = last + init + "@rhodes.edu" return address def main(): firstname = input("First name: ") lastname = input("Last name: ") addr = make_prof_email(firstname, lastname) print("Email:", addr)
SLIDE 20 You ¡try ¡it ¡
- Write ¡a ¡funcDon ¡make_student_email ¡that ¡
creates ¡(and ¡returns) ¡a ¡student ¡email ¡address. ¡
- The ¡funcDon ¡should ¡take ¡four ¡parameters: ¡first ¡
name, ¡last ¡name, ¡middle ¡name, ¡and ¡class ¡year. ¡
- Challenge: ¡Modify ¡the ¡funcDon ¡so ¡it ¡takes ¡only ¡two ¡
parameters: ¡someone's ¡full ¡name ¡(one ¡string ¡with ¡ first, ¡middle, ¡and ¡last ¡names ¡within ¡it) ¡and ¡class ¡
SLIDE 21
- A ¡fundamental ¡problem ¡when ¡using ¡strings ¡is ¡
compuDng ¡a ¡substring, ¡or ¡a ¡string ¡slice. ¡
- We ¡want ¡to ¡tell ¡Python ¡
– take ¡some ¡string, ¡ – give ¡me ¡all ¡the ¡characters ¡starDng ¡from ¡one ¡index, ¡ – and ¡ending ¡at ¡another ¡index. ¡
- Fortunately, ¡this ¡is ¡built ¡into ¡Python! ¡
SLIDE 22
- Two ¡ways ¡to ¡use ¡square ¡brackets. ¡
- 1 ¡number ¡inside ¡the ¡brackets: ¡
– returns ¡exactly ¡one ¡character ¡of ¡a ¡string. ¡ – if ¡s ¡= ¡"Computer, ¡then ¡s[0] ¡returns ¡"C" ¡
- 2 ¡numbers ¡inside ¡the ¡brackets: ¡
– returns ¡a ¡substring ¡or ¡string ¡slice. ¡ ¡
SLIDE 23
s[a:b] ¡gives ¡you ¡a ¡string ¡slice ¡of ¡string ¡s ¡starDng ¡ from ¡index ¡a ¡and ¡ending ¡at ¡index ¡b-‑1. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡0 ¡ ¡ ¡ ¡ ¡ ¡ ¡1 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡2 ¡ ¡ ¡ ¡ ¡ ¡ ¡3 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡4 ¡ ¡ ¡ ¡ ¡ ¡ ¡5 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡6 ¡ ¡ ¡ ¡ ¡ ¡ ¡7 ¡ ¡ s[0:1] ¡-‑> ¡"C" ¡ ¡just ¡like ¡s[0] ¡ s[0:2] ¡-‑> ¡"Co" ¡ s[0:7] ¡-‑> ¡"Compute" ¡ s[3:6] ¡-‑> ¡"put" ¡ s[0:8] ¡-‑> ¡"Computer" ¡ "C" ¡ "o" ¡ "m" ¡ "p" ¡ "u" ¡ "t" ¡ "e" ¡ "r" ¡
SLIDE 24 More ¡fun ¡with ¡indices ¡
- Indices ¡can ¡also ¡be ¡negaDve. ¡
- A ¡negaDve ¡index ¡counts ¡from ¡the ¡right ¡side ¡of ¡
the ¡string, ¡rather ¡than ¡the ¡leb. ¡
s = "Computer" print(s[-1]) # prints r print(s[-3:len(s)]) # prints ter print(s[1:-1]) # prints ompute
SLIDE 25
- Slices ¡don't ¡need ¡both ¡leb ¡and ¡right ¡indices. ¡
- Missing ¡leb ¡index: ¡
– Python ¡assumes ¡you ¡meant ¡0 ¡[far ¡leb ¡of ¡string] ¡
– Python ¡assumes ¡you ¡meant ¡len(s) ¡[far ¡right ¡of ¡string] ¡
s = "Computer" print(s[1:]) # prints omputer print(s[:5]) # prints Compu print(s[-2:]) # prints er
SLIDE 26
Indices ¡don't ¡have ¡to ¡be ¡literal ¡numbers ¡
Say ¡we ¡have ¡this ¡code: ¡ name = input("type in your name: ") x = int(len(s) / 2) print(name[0:x]) What ¡does ¡this ¡print? ¡