Strings 1

Calvin (Deutschbein)

W4Wed: 18 Sep


Announcements

  • By next MONDAY 11:59 PM: "Problem Set 3: Strings" assignment.
    • It is about strings
    • Start now.
    • We want to get ahead of the clock on problem sets since there's a project soon.
  • Sections today and tomorrow. Also on strings.

Today

  • Strings
  • String arithmetic
    • Addition / Multiplication
    • Comparison

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

  • Strings rule, they are basically what I use Python for the most. >>> for student in cs_majors: ... if 'Sophomore' in student: ... x += 1
  • For example, I can count how many sophomores have declared a CS major.
    • If you want to declare a CS major, ckdeutschbein@willamette.edu

Strings

  • Python is pretty good at numbers. >>> 1234 * 5678 7006652
  • But could be better >>> 9.9 ** 20 + 1000 == 9.9 ** 20 True

Strings

  • I do not know how Python could be better with strings.
  • I copy-pasted the entirety of Pride and Prejudice into Python >>> letters = 0 >>> for letter in book: ... letters += 1 ... >>> letters 1496244

Strings

  • I copy-pasted the entirety of Pride and Prejudice into Python >>> es = 0 >>> for letter in book: ... if 'e' == letter: ... es += 1 ... >>> es 147006
  • There are 147006 letter 'e' characters in Pride and Prejudice.
  • That seems... hard to count otherwise.
  • This sees use in e.g. State Dept analysis, digital humanities, etc.

Strings 1

  • Four ways to make a variable that is a string.
    • 1. Use single quotes. name = 'calvin'

Strings 2

  • Four ways to make a variable that is a string.
    • 1. Use single quotes. name = 'calvin'
    • 2. Use double quotes, which allows counting single quotes. name = "L'Ouverture"

Strings 3

  • Four ways to make a variable that is a string.
    • 1. Use single quotes. name = 'calvin'
    • 2. Use double quotes, which allows counting single quotes. name = "L'Ouverture"
    • 3. Use three double quotes, which allows spanning multiple lines book = """It is a truth universally acknowledged, that a single man in possession of a good fortune must be in want of a wife. However little known the feelings or views of such a man may be on his first entering a neighbourhood, this truth is so well fixed in the mind"""

Strings 4

  • Four ways to make a variable that is a string.
    • 1. Use single quotes. name = 'calvin'
    • 2. Use double quotes, which allows counting single quotes. name = "L'Ouverture"
    • 3. Use three double quotes, which allows spanning multiple lines book = """It is a truth universally acknowledged, that a single man in possession of a good fortune must be in want of a wife. However little known the feelings or views of such a man may be on his first entering a neighbourhood, this truth is so well fixed in the mind"""
    • 4. Use the str() function, which makes a non-string into a string. number = str(105 * 4)

Strings 4 Again

  • Emphasis
  • 4. Use the str() function, which makes a non-string into a string. number = str(105 * 4)
  • Be advised:
    • The string "10" and the value 10 (no quotes) are not equal. >>> str(10) == 10 False
    • Always check with type()! >>> x = 10 >>> type(x) <class 'int'> >>> x = str(x) >>> type(x) <class 'str'>
    • Use type()!

Today

  • : Strings
  • String arithmetic
    • Addition / Multiplication
    • Comparison

String +

  • For some reason, Python let's us add strings together.
    • You can decide if you like this or not. It's a bit unusual.
    >>> print('CS' + '151') # there won't be spaces... CS151
  • In general, Python makes sure you don't add strings to ints. >>> print('CS' + 151) # won't work Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can only concatenate str (not "int") to str
  • I am confident you can read and understand this error code if you see it.
  • Read the the last line.

String +

  • If we intend to add a string and an integer, we just have to say so. >>> number = 151 >>> prefix = 'CS' >>> prefix + str(number) 'CS151'
  • The most common case for me: adding spaces. >>> number = 151 >>> prefix = 'CS' >>> prefix + ' ' + str(number) 'CS151'
  • NOTE: I pretty much always forget a plus '+' on one side of the space.
  • Subtraction does not work
    • What would it even do?

String *

  • I do a lot better reading things when they align vertically, like I did here with 'number' and 'prefix':
    • That is, the single-equals assignment '=' is vertical aligned, one over another
    >>> number = 151 >>> prefix = 'CS'

String *

  • I also teach classes called:
    • CS 151
    • IDS 101
    • DATA 599
  • This is hard on my eyes - it isn't easy to see, for example, DATA 599 is a graduate class (500-level)
  • I solve this by... adding spaces!
    • This is called 'right padding' is one of the most used techniques in text processing!

len()

  • Python includes a handy built-in function called len() to help us here.
    • It is handy in the way print() and type() are - keep it in mind!
  • We can actually use it on more than just strings... >>> len(range(0,50,7)) # 0, 7, 14, 21... 28, 35, 42, 49 8

len()

  • Python includes a handy built-in function called len() to help us here.
  • It works very well on strings. >>> letters 1496244 >>> len(book) 1496244
  • Ranges and strings have other commonalities - more latter.

right pad

  • On like my course websites, etc., I right pad my course names: def right_pad(prefix, number): if len(prefix) == 2: # CS return prefix + " " + number if len(prefix) == 3: # IDS return prefix + " " + number if len(prefix) == 4: # DATA return prefix + " " + number if len(prefix) == 5: # PHEAL return prefix + number
  • This is what is known as bad code (I would make too many typos)
  • We are allowed to make bad code as long as we want to make it better!

right pad

    def n_spaces(n): s = "" for i in range(n): s = s + " " return s def right_pad(prefix, number): if len(prefix) == 2: # CS return prefix + n_spaces(3) + number if len(prefix) == 3: # IDS return prefix + n_spaces(2) + number if len(prefix) == 4: # DATA return prefix + n_spaces(1) + number if len(prefix) == 5: # PHEAL return prefix + n_spaces(0) + number

right pad

    def n_spaces(n): s = "" for i in range(n): s = s + " " return s def right_pad(prefix, number): # wait a minute... num_spaces = 5 - len(prefix) # the longest is 5 return prefix + n_spaces(num_spaces) + number

right pad

  • By the way, we have a name for adding things 'n' times.
  • It's multiplication. def n_spaces(n): return " " * n def right_pad(prefix, number): # wait a minute... num_spaces = 5 - len(prefix) # the longest is 5 return prefix + n_spaces(num_spaces) + number
  • For some reason, multiplying strings is allowed.
  • This is a good way to be cute, among other things. >> print("hi" * 3) # mfw i see bae hihihi

Today

  • : Strings
  • String arithmetic
    • : Addition / Multiplication
    • Comparison

String comparison

  • We can use comparison operators on strings.
  • They just do alphabetical, and test it if it seems non-obvious.
  • Vocab word: This is called "lexicographical" ordering (term might be on midterm).
  • I think
    • "a" is before "b", and
    • "1" is before "2", so
    • "a" < "b" since "1" < "2"

String comparison

  • Just test it.
    • Alphabetical ('a' before 'b')
    • Capitals before lower
    • Numbers before letters
    • Shorter before longer
  • Can't compare strings and ints.
  • Don't do what I always do and confuse strings with variable names. >>> a = 'something' >>> b = 'another thing' >>> a < b False
  • Use descriptive variable names.
>>> 1 < 2 # baseline True >>> 'a' < 'b' True >>> 'a' < 'A' False >>> 'a' < '1' False >>> 'A' < '1' False >>> 'a' < 'aa' True >>> 'Oregon' < 'Washington' True >>>

Today

  • : Strings
  • : String arithmetic
    • : Addition / Multiplication
    • : Comparison

Announcements

  • By MONDAY 11:59 PM: "Problem Set 2: Checkers" assignment.
    • One Karel problem (probably) using "if" and "while"
    • One Python problem using arithmetic and (probably) "for" def divisors(n:int) -> int: # this is useful count = 0 for x in range(2,n): if n % x == 0: print(x) count = count + 1 return count
    • One writing problem.

Announcements

  • By MONDAY 11:59 PM: "Problem Set 3: Strings" assignment.
    • It is about strings
    • Start now. def n_spaces_before(n:int,s:str) -> NoneType: # print -> None print(n * ' ', s) # test - that is 'n times space plus s'
    • We want to get ahead of the clock on problem sets since there's a project soon.
  • Sections today and tomorrow. Also on strings.