# Strings --- CS 65 // 2021-03-09 ## Extra Credit Activities - [The Anderson Gallery](https://linktr.ee/theandersongallery) + 50th Annual Juried Student Exhibition + [Virtual Opening and Awards Ceremony](https://www.youtube.com/watch?v=yvlpDFYudVg) + [Virtual Performance and Lecture](https://www.youtube.com/channel/UCPCCKI0rPk97vpY61BvCK9w/videos) (3/10) at 7pm ## Extra Credit Activities - MathCS Special Interest Group + Microsoft Team to help foster community between the students and faculty interested in topics related to Mathematics and Computer Science # CS 65 Tutoring # Questions ## ...about anything? # Loops Lab: Debrief # Strings ## The `str` Type - **Purpose**: + To manipulate "text" (sequences of characters) - **Express:** + Strings are written: `"abc"` or `'abc'` - **Operations:** + `s1 + s2`: concatenates two strings together + `s * num`: concatenates `num` copies of `s` together ## The `str` Type - **Other Operations**: + `len(s)`: gets the **length** of the string `s` + `s[index]`: gets the character at the given index + `s[start:end]`: gets the substring that starts at index `start` and ends before index `end` + `s.upper()`: gets a copy of `s` where each lowercase character is replaced by its uppercase + `s.lower()`: gets a copy of `s` where each uppercase character is replaced by its lowercase + `s1 in s2`: returns true if `s1` is a substring of `s2` ## Unicode - Unicode is an **international standard** for encoding characters - In particular, every character is given an associated number called an **ordinal** - In Python, you can turn a character into its ordinal number using the `ord(ch)` function - You can also turn numbers into their associated character using the `chr(ch)` function ## Mutability / Immutability - Many basic values in Python are **immutable**, i.e., they cannot be changed after they are created + `int`s, `float`s, and `str`s are all immutable + Thus, expressions `s[2:7]` constructs a **new string** rather than modifying `s` - Graphics objects are **mutable**---commands such as `rect.move(50,0)` mutate the object rather than create a new rectangle ## Traversal with a `while` Loop - We can iterate over string using a while loop such as: ```py fruit = "banana" index = 0 while index < len(fruit): letter = fruit[index] print(letter) index = index + 1 ``` ## Tranversal with a `for` Loop - We can also use a `for` loop to traverse a list ```py fruit = "banana" for letter in fruit: print(letter) ``` # Self Checks # Lab Time