Strings 2

Calvin (Deutschbein)

W4Fri: 20 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

  • Remember how we could do this: >>> x = "hello world" >>> print(x) hello world
  • This is snippets of text in Python are called 'strings' >>> type(x) <class 'str'>

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) ... 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) ... c s
    • 2. Use a 'for' loop over the length of the string... >>> for i in range(len('cs')): ... print('cs'[i]) # get it? csi? ... c s
    • 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
  • We can test: >>> i_before_e('veil') False >>>

Strings

  • Example: 'i' before 'e'
  • This breaks on 'lied': >>> i_before_e('lied') >>>
  • 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). 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
    >>> "hello world"[-3] 'r' >>> "hello world"[8] '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]
  • This was as close as I could get to a word: "jumps" no vowels. >>> print(new_word) 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: >>> abcs[9:20:3] 'jmps' >>>

Strings

  • Recall: This is start, end, step.
  • Like range, we can leave some things out. (Omit start/end to get "the rest" >>> "hello world"[:5] 'hello' >>> "hello world"[5:] ' world'
  • Reverse a string with a negative step: >>> "deeps"[::-1] 'speed'

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
  • Reverse a string with a negative step: >>> remove_space("hello world") 'helloworld'

Strings

  • You can do other changes, like
  • 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:]
  • Reverse a string with a negative step: >>> space_to_underscore("hello world") 'hello_world'
  • This only works on the first space. >>> space_to_underscore("world") >>>

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
  • Reverse a string with a negative step: >>> space_to_underscore("hello world") 'hello_world'
  • This only works on the first space. >>> space_to_underscore("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
  • We already know how to remove one space, so we just remove the next space the same way. >>> spaces_to_underscores("thats me espresso") '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