@extends('template') @section('title') Lab 4, Part 1: Predicates and Functions @stop @section('content') # Lab 4, Part 2: Working with Strings ## How to take apart and put together strings ![](cs111beads.gif) It's useful to know how to extract parts of strings. First, let's set up some string variables (you can type these directly into your Canopy interactive window -- it's better to type than to copy and paste because the indentation will generate errors). Or you can copy them with your mouse and paste them into a file, and then run the file. ```py theTruth = 'CS111 rocks my world!' name = 'Tina Turner' line1 = 'What\'s love got to do with it' # note the backslash ``` Note: does it matter if you use single quotes (') or double quotes (") in pyt hon around strings? Short answer: no. [Click here for a long explanation.](https://docs.python.org/2/tutorial/introduction.html#strings) Now, try out these python commands and see what you get: ```py theTruth[0] name[0] name[1:5] name[8:10] ``` Let's get a little bit tricker with our string examples: ```py name[-1] name[-3] line1[::-1] line1[len(line1)] # oops. Why error message? ``` Note that we use the built-in python function `len` in the examples above. Here is a list of all the [built-in python functions](https://docs.python.org/2/library/functions.html). Here are some special python string operators (`in` and `not in`): ```py 'leia' in str2 ==> False 'our' in str2 ==> True 'yoda' not in str2 ==> True ``` --- ## Table of some string operations
symbol operation example
+ concatenation str1 = 'I am '
str2 = 'your father '
darth = str1 + str2
yoda = str2 + str1
* repetition 'hee'*3 ==> 'heeheehee'
[] slice str2[5]==> 'f'
[:] range str2[2:5]==> 'ur '
in in 'dad' in str2 ==> False
'at' in str2 ==> True
not in not in str1 not in str2 ==> True
str1 not in darth ==> False
## Write some simple string predicates ## Task 2A. `endsWithA` returns True if the string ends with the letter a; False otherwise. *Hint:* `word[-1]` *gives the last letter of a word, e.g.* `rain[-1]` returns `n`. Examples: ```py endsWithA('Lyn') ==> False endsWithA('Justin') ==> False endsWithA('Justina') ==> True endsWithA('Mia') ==> True endsWithA('ANNA') ==> True ``` ## Task 2B. `hasPop` returns True if the string contains the substring 'pop' ; False otherwise. Examples: ```py hasPop('popcorn') ==> True hasPop('lollipop') ==> True hasPop('hippopotamus') ==> True hasPop('poopy') ==> False ``` ## Task 2C. Writing simple string functions Add the functions below to your `lab04.py` file. You may, if you like, use the built-in Python function `len(s)`, which returns the length of a string `s`. Copy and paste the function definitions template below into your `lab04.py` file. You'll need to fill in the missing code to make sure that your code works correctly. Note that each function should be just one line of code. ```py def first(s): """returns the first letter of the given string e.g. first("tahoe") => t""" # ____________________ # replace the blank line above with your code def last(s): """returns the last letter of the given string e.g. last("tahoe") => e""" # ____________________ # replace the blank line above with your code def sameLength (s1, s2): """determines whether strings s1 and s2 have the same length e.g. sameLength("tahoe","snow") => False e.g. sameLength("cold","snow") => True""" # ___________________ # replace the blank line above with your code def average(a,b): """returns the average of two numbers e.g. average(9,12) => 10.5""" # ____________________ # replace the blank line above with your code ``` ## Task 2D. Writing more complex predicates In this task, you'll write the following three string predicates (include them in your `lab04.py` file): 1. **`isBookend(s)`**: A string is a bookend if it begins and ends with the same letter. For example, "neon" is a bookend and "cake" is not a bookend. 2. ** `siblings(s1,s2)`**: Two strings are siblings if * they have the same length and * they either begin or end with the same letter. For example, "Emily" and "Elise" and siblings; "Tucker" and "Oliver" are siblings, but "Sam" and "Scott" are not and "hi" and "hawaii" are not. 3. **`stringAverage(s1,s2,s3)`**: Checks to see if the middle string has a length that i s the average of the other two strings. For example, "bowling" has a length that is the average of the length of the strings "football" and "soccer". Write the predicates one at a time, testing each one to convince yourself that it works before moving on to the next one. @include('/labs/lab04/_toc') @stop