Day 3 Lesson 11 Using strings and sequences



![Notes on indexing Python uses zero-based indexing print fruit[0] Brackets vs. parenthesis Use brackets Notes on indexing Python uses zero-based indexing print fruit[0] Brackets vs. parenthesis Use brackets](https://slidetodoc.com/presentation_image/997bdf76b87dd7be9972279538880fe9/image-4.jpg)






![Slicing a sequence What do these do? print fruit[3: 3] print fruit[: ] 11 Slicing a sequence What do these do? print fruit[3: 3] print fruit[: ] 11](https://slidetodoc.com/presentation_image/997bdf76b87dd7be9972279538880fe9/image-11.jpg)

![A mutable sequence: list x = [1, 2, 3] print x x[1] = 4 A mutable sequence: list x = [1, 2, 3] print x x[1] = 4](https://slidetodoc.com/presentation_image/997bdf76b87dd7be9972279538880fe9/image-13.jpg)
![Are strings mutable? x = 'perrot' print x x[1] = 'a' print x 14 Are strings mutable? x = 'perrot' print x x[1] = 'a' print x 14](https://slidetodoc.com/presentation_image/997bdf76b87dd7be9972279538880fe9/image-14.jpg)
!["Changing" a string x = 'perrot' x = x[: 1] + 'a' + x[2: "Changing" a string x = 'perrot' x = x[: 1] + 'a' + x[2:](https://slidetodoc.com/presentation_image/997bdf76b87dd7be9972279538880fe9/image-15.jpg)

![The len function Example length = len(fruit) last = fruit[length] print last 17 Python The len function Example length = len(fruit) last = fruit[length] print last 17 Python](https://slidetodoc.com/presentation_image/997bdf76b87dd7be9972279538880fe9/image-17.jpg)
![The len function Example length = len(fruit) last = fruit[length-1] print last 18 Python The len function Example length = len(fruit) last = fruit[length-1] print last 18 Python](https://slidetodoc.com/presentation_image/997bdf76b87dd7be9972279538880fe9/image-18.jpg)










- Slides: 28

Day 3 – Lesson 11 Using strings and sequences Python Mini-Course University of Oklahoma Department of Psychology 1 Python Mini-Course: Day 3 – Lesson 11 5/02/09

Lesson objectives 1. Understand how Python stores and uses strings 2. Perform indexing and slicing operations on Python sequences 3. Traverse strings with a loop 4. Compare strings and substrings 2 Python Mini-Course: Day 3 – Lesson 11 5/02/09

Strings in Python A string is a sequence of characters Sequences are indexed fruit = 'banana' letter = fruit[1] print letter 3 Python Mini-Course: Day 3 – Lesson 11 5/02/09
![Notes on indexing Python uses zerobased indexing print fruit0 Brackets vs parenthesis Use brackets Notes on indexing Python uses zero-based indexing print fruit[0] Brackets vs. parenthesis Use brackets](https://slidetodoc.com/presentation_image/997bdf76b87dd7be9972279538880fe9/image-4.jpg)
Notes on indexing Python uses zero-based indexing print fruit[0] Brackets vs. parenthesis Use brackets [x] for indexing Use parenthesis (x) for function calls 4 Python Mini-Course: Day 3 – Lesson 11 5/02/09

Notes on indexing You can use any expression as an index, provided it has an integer value fruit = 'banana' a, b = 1, 3 print fruit[b-a] 5 Python Mini-Course: Day 3 – Lesson 11 5/02/09

Notes on indexing You can use any expression as an index, provided it has an integer value fruit = 'banana' a, b = 1. 0, 3. 0 print fruit[a-b] 6 Python Mini-Course: Day 3 – Lesson 11 5/02/09

Notes on indexing Negative indices count backward from the end of the sequence fruit = 'banana' print fruit[-1] print fruit[-2] 7 Python Mini-Course: Day 3 – Lesson 11 5/02/09

Slicing a sequence You can specify a range of indices to slice a sequence fruit = 'banana' print fruit[1: 3] 8 Python Mini-Course: Day 3 – Lesson 11 5/02/09

Slicing a sequence For slicing, imagine the indices as pointing between the characters 9 Python Mini-Course: Day 3 – Lesson 11 5/02/09

Slicing a sequence To slice from the beginning of the sequence, omit the first index print fruit[: 3] To slice from the end of the sequence, omit the last index print fruit[3: ] 10 Python Mini-Course: Day 3 – Lesson 11 5/02/09
![Slicing a sequence What do these do print fruit3 3 print fruit 11 Slicing a sequence What do these do? print fruit[3: 3] print fruit[: ] 11](https://slidetodoc.com/presentation_image/997bdf76b87dd7be9972279538880fe9/image-11.jpg)
Slicing a sequence What do these do? print fruit[3: 3] print fruit[: ] 11 Python Mini-Course: Day 3 – Lesson 11 5/02/09

Mutability In Python, some types of sequences can be changed These are mutable Others cannot be changed These are immutable 12 Python Mini-Course: Day 3 – Lesson 11 5/02/09
![A mutable sequence list x 1 2 3 print x x1 4 A mutable sequence: list x = [1, 2, 3] print x x[1] = 4](https://slidetodoc.com/presentation_image/997bdf76b87dd7be9972279538880fe9/image-13.jpg)
A mutable sequence: list x = [1, 2, 3] print x x[1] = 4 print x Here, x is a list. We'll learn more about lists next week. 13 Python Mini-Course: Day 3 – Lesson 11 5/02/09
![Are strings mutable x perrot print x x1 a print x 14 Are strings mutable? x = 'perrot' print x x[1] = 'a' print x 14](https://slidetodoc.com/presentation_image/997bdf76b87dd7be9972279538880fe9/image-14.jpg)
Are strings mutable? x = 'perrot' print x x[1] = 'a' print x 14 Python Mini-Course: Day 3 – Lesson 11 5/02/09
![Changing a string x perrot x x 1 a x2 "Changing" a string x = 'perrot' x = x[: 1] + 'a' + x[2:](https://slidetodoc.com/presentation_image/997bdf76b87dd7be9972279538880fe9/image-15.jpg)
"Changing" a string x = 'perrot' x = x[: 1] + 'a' + x[2: ] print x The + sign is a concatenation operator for sequences NB: The above code actually creates a new string and assigns it to x 15 Python Mini-Course: Day 3 – Lesson 11 5/02/09

The len function Syntax len(sequence) Returns the number of items in a sequence NB: because of zero-based indexing, the last valid index is one less than the length 16 Python Mini-Course: Day 3 – Lesson 11 5/02/09
![The len function Example length lenfruit last fruitlength print last 17 Python The len function Example length = len(fruit) last = fruit[length] print last 17 Python](https://slidetodoc.com/presentation_image/997bdf76b87dd7be9972279538880fe9/image-17.jpg)
The len function Example length = len(fruit) last = fruit[length] print last 17 Python Mini-Course: Day 3 – Lesson 11 5/02/09
![The len function Example length lenfruit last fruitlength1 print last 18 Python The len function Example length = len(fruit) last = fruit[length-1] print last 18 Python](https://slidetodoc.com/presentation_image/997bdf76b87dd7be9972279538880fe9/image-18.jpg)
The len function Example length = len(fruit) last = fruit[length-1] print last 18 Python Mini-Course: Day 3 – Lesson 11 5/02/09

Traversing a sequence Often, we want to do something to every item in a sequence We need to traverse the sequence This can be done with a loop 19 Python Mini-Course: Day 3 – Lesson 11 5/02/09

Using a while loop: traverse 1. py def traverse(string): index = 0 while index < len(string): letter = string[index] print letter index += 1 traverse('Monty Python') 20 Python Mini-Course: Day 3 – Lesson 11 5/02/09

Using a for loop: traverse 2. py def traverse(string): for letter in string: print letter traverse('Monty Python') 21 Python Mini-Course: Day 3 – Lesson 11 5/02/09

Searching strings: find. py def find(word, letter): index = 0 while index < len(word): if word[index] == letter: return index = index + 1 return -1 22 Python Mini-Course: Day 3 – Lesson 11 5/02/09

Searching strings: count. py def count(word, letter): count = 0 for item in word: if item == letter: count += 1 return count('banana', 'a') 23 Python Mini-Course: Day 3 – Lesson 11 5/02/09

String comparison To compare whole strings, use the standard comparison operators == < > <= >= NB: strings are compared using numeric codes (e. g. , ASCII), so case is very important 24 Python Mini-Course: Day 3 – Lesson 11 5/02/09

String comparison (try this on the command line of IDLE) x, y, z = 'abc', 'Abc', 'a. Bc' x == y x < y x > y x < z x > z 25 Python Mini-Course: Day 3 – Lesson 11 5/02/09

The in operator When used in a for statement, in iterates through a sequence However, in is also a Boolean operator that checks membership within a sequence 'a' in 'banana' 26 Python Mini-Course: Day 3 – Lesson 11 5/02/09

Comparing strings: string_comp. py def compare(string 1, string 2): if string 1 in string 2: print string 1 + ' is a substring of ' + string 2 if string 2 in string 1: print string 2 + ' is a substring of ' + string 1 if string 1 == string 2: print string 1 + ' equals ' + string 2 elif string 1 > string 2: print string 1 + ' comes after ' + string 2 else: print string 1 + ' comes before ' + string 2 27 Python Mini-Course: Day 3 – Lesson 11 5/02/09

Comparing strings compare('apple', 'banana') compare('banana', 'Pineapple') compare('banana', 'ana') compare('banana', 'banana') 28 Python Mini-Course: Day 3 – Lesson 11 5/02/09