Lecture 2 (Spring 2023)
#

Required readings
#

  • Sections 2.4 – 2.8
  • Chapter 3

Strings
#

Strings are sequences of case-sensitive characters, used to hold text-like data. Strings have many operations defined on them:

  • concatenation. (
    <str> + <str>
    ) joins two strings together.
s1 = "hello"
s2 = "world"
s3 = s1 + s2       # s3 = "helloworld"
s4 = s1 + " " + s2 # s4 = "hello world"
  • multiplication. (
    <str> * <int>
    ) repeats a string multiple times.
s1 = "hello"
s2 = s1 * 4   # s2 = "hellohellohellohello"

We used the same operator on different object types, and those operators performed different functions. Operator overloading is when operations have different functions depending on the object types provided to them.

Other string operations:

  • len
    . returns the length of a string.
  • s1 in s2
    .
    True
    if
    s1
    is a substring of
    s2
    otherwise
    False
    .
  • Comparisons (
    ==
    ,
    <
    ,
    >
    ). Greater than and less than are based on Unicode order.

Indexing and slicing
#

Strings are immutable
#

Strings as output: f-strings
#

String as input:
input()
#

Booleans, conditionals, and branching
#

Iteration (looping)
#

for
loops,
range
#

while
loops
#

break
and
continue
#

Guess-and-check algorithms
#