Strings 2

Calvin (Deutschbein)

W4Fri: 19 Sep


Announcements

  • By MONDAY 11:59 PM: "Problem Set 3: Strings" assignment.
    • One on string multiplication.
    • One on string indices/slices.
    • One on string loops.

Today

  • String loops
  • String indices
  • String slices

Strings

  • Two ways to look at a single character/letter in a string.
    • 1. Use a 'for' loop over the string. for letter in "cs": print(letter)
    • Print places each thing on it's own line.
    • Each "thing" is a letter. c s

Strings

  • Example: remove vowels:
    • Use a 'for' loop over the string. for letter in "abcdef": print(letter)
    • What will this print? a b c d e f
    • Say we wish to examine values. We use 'in'

Strings

  • Example: remove vowels:
    • We use the special "in" keyword. vowels = "aeiou" for letter in "abcdef": print(letter, letter in vowels)
    • What will this print? a True b False c False d False e True f False
    • Say we wish to remove values. We use 'if'

Strings

  • Example: remove vowels:
    • We use the special "if" keyword. vowels = "aeiou" for letter in "abcdef": if letter not in vowels: print(letter)
    • What will this print? b c d f

Strings

  • Two ways to look at a single character/letter in a string.
    • 1. Use a 'for' loop over the string. for letter in "cs": print(letter)
    • 2. Use a 'for' loop over the length of the string... for i in range(len('cs')): print('cs'[i]) # get it? csi?
    • Time to talk about indices

Strings

  • Example: 'i' before 'e'
  • Use a 'for' loop over the length of the string... word = "lied" # word = "veil" for i in range(len(word)): print(word[i])
  • See what prints: l i e d

Strings

  • Example: 'i' before 'e'
  • We are only worried about 'e' and 'i' word = "lied" # word = "veil" for index in range(len(word)): if word[index] == 'i': # check the letter *before* the 'i' print(word[index-1])
  • See what prints: l
  • 'i' never follows an 'e', so this is okay!

Strings

  • Example: 'i' before 'e'
  • We are only worried about 'e' and 'i' # word = "lied" word = "veil" for index in range(len(word)): if word[index] == 'i': # check the letter *before* the 'i' print(word[index-1])
  • See what prints: e
  • 'i' follows an 'e', so this is bad!

Strings

  • Example: 'i' before 'e'
  • Instead of printing 'e' (or not) we return false. def i_before_e(word:str) -> bool: for index in range(len(word)): if word[index] == 'i': # check the letter *before* the 'i' if word[index-1] == 'e': return False print(i_before_e('veil'))
  • We can test: False

Strings

  • Example: 'i' before 'e'
  • This breaks on 'lied': print(i_before_e('lied'))
  • This should be true... None
  • Why? We don't return True ever - we should if we get to the *end of the string* (then we know we have no more i's that might be after e's).

Strings

  • Quick fix: def i_before_e(word:str) -> bool: for index in range(len(word)): if word[index] == 'i': # check the letter *before* the 'i' if word[index-1] == 'e': return False return True # this is indented the same as the 'for' loop

Strings

  • Example: 'i' before 'e'
  • Also - we have to make sure it works if 'i' is the first letter. >>> i_before_e('icee') # its a disaster
  • 'i' is in position 0, so i_before_e checks negative one.
  • This checks the end of the list - where there is an 'e'.
  • def i_before_e(word:str) -> bool: for index in range(1, len(word)): # fix is here. if word[index] == 'i' and word[index-1] == 'e': return False return True # this is indented the same as the 'for' loop

Strings

  • Negative indicies.
    helloworld
    012345678910
    -10-9-9-8-7-6-5-4-3-2-1
  • We can try this: print("hello world"[-3]) print("hello world"[8])
  • These are two ways to get to the same place, like 3 lefts vs. 1 right. r r

Today

  • : String loops
  • : String indices
  • String slices

Strings

  • This is annoying: if word[index] == 'i' and word[index-1] == 'e':
  • It doesn't have to be.
  • Strings are like ranges, and we give an index like we give a range.

Strings

  • We can use ranges to see certain letters: abcs = 'abcdefghijklmnopqrstuvwxyz' for i in range(9,20,3): print(i, abcs[i])
  • This was as close as I could get to a word: "jumps" no vowels. 9 j 12 m 15 p 18 s

Strings

  • We can use this with "+" to make new words... abcs = 'abcdefghijklmnopqrstuvwxyz' new_word = "" for i in range(9,20,3): new_word += abcs[i] print(new_word)
  • This was as close as I could get to a word: "jumps" no vowels. jmps

Strings

  • This: new_word = "" for i in range(9,20,3): new_word += abcs[i]
  • is the same as this: new_word = abcs[9:20:3]
  • Check it out: print(abcs[9:20:3])
  • What do you see?

Strings

  • Recall: This is start, end, step.
  • Like range, we can leave some things out. (Omit start/end to get "the rest"
    print("hello world"[:5]) print("hello world"[5:]) print("deeps"[::-1]) hello world speed
  • Note the space before " world"!

Strings

  • Mostly useful on homework to check the rest of string for something (repeated letters)
  • Remove spaces: def remove_space(s:str) -> str: # given string, make new string for i in range(len(s)): # check each letter if s[i] == " ": # if letter is space... return s[:i] + s[i+1:] # before space + after space print(remove_space("hello world"))
  • This almost works: helloworld

Strings

  • Remove spaces: def remove_space(s:str) -> str: # given string, make new string for i in range(len(s)): # check each letter if s[i] == " ": # if letter is space... return s[:i] + s[i+1:] # before space + after space print(remove_space("world"))
  • What if there's no space? Well, nothing prints at all!

Strings

  • Remove spaces: def space_to_underscore(s:str) -> str: for i in range(len(s)): # check each letter if s[i] == " ": # if letter is space... return s[:i] + "_" + s[i+1:] return s # return 's' if no spaces print(space_to_underscore("hello world")) print(space_to_underscore("world")
  • This only works on the first space. hello_world world

Strings

  • Remove spaces: def spaces_to_underscores(s:str) -> str: for i in range(len(s)): # check each letter if s[i] == " ": # if letter is space... return s[:i] + "_" + spaces_to_underscores(s[i+1:]) return s # return 's' if no spaces print(spaces_to_underscores("thats me espresso"))
  • We already know how to remove one space, so we just remove the next space the same way. thats_me_espresso
  • This works on any number of spaces (before only worked on exactly one).

Announcements

  • By MONDAY 11:59 PM: "Problem Set 3: Strings" assignment. def n_spaces_before(n:int,s:str) -> NoneType: # print -> None print(n * ' ', s) # test - that is 'n times space plus s' def spaces_to_underscores(s): for i in range(len(s)): if s[i] == " ": return s[:i] + "_" + spaces_to_underscores(s[i+1:]) return s