Strings as a Compound Data Type
Strings as a Sequence of Characters
- we can treat a string as a sequence of characters
- each character can be referred to by its place in the string
- this place is a numeric value called an index
- an index specifies the position of an item in an ordered collection (in the context of strings, a sequence of characters)
- indexes start at 0, not at 1
- consequently, the first character of a string is at index 0
- for example, in the string "hi", h is at index 0, i is at index 1
String Indexes
Let's take the string "cool cat!". What is the index of the first 'c'? What character is at index 1. How about index 4? →
- the first 'c' is at index 0
- the first 'o' is at index 1
- space (' ') is at index 4
The Last Index
What index is the last character of each of these strings? →
- "foo"
- "hi"
- "!"
The last index is the length of the string - 1
- 2
- 1
- 0
Some More Index Exercises
- What's the index of 'h' in "hello"?
- What's the index of 'o' in "hello"?
- What's the index of the second 'l' in "hello"?
- What's the letter at index 0 in "hi there."?
- What's the letter at index 2 in "hi there."?
- 'h' is index 0 in "hello"
- 'o' is index 4 in "hello"
- the second 'l' is index 3 in "hello"
- 'h' is index 0 in "hi there."
- ' ' (space) is index 2 in "hi there"
Indexing Into a String
You can reference a specific character in a string (indexing) by:
- using the index (an integer)
- enclosed by square brackets
- immediately following the string
- returns a new string consisting of only that one character
- note that the index or string can be variables or expressions!
Indexing Examples
Indexing Exercises
What does the following code output. Let's walk through it line by line. →
Negative Indexes Also Work
-1 is the index of the last letter, -2, second to last, etc. …What does the following program output? Let's go through it line by line. →
One Last Note About Indexing
- strings are not mutable
- you can read values by indexing into a string
- however, you can't change values
- consequently, you can't set an index equal to another character
- let's see what happens when we try to change a character at an index using the assignment (=) operator →
Using Every Character in a String
What if we want to do something to each character in a string? Manually and explicitly using indexes would be time consuming! What if I wanted to:
- count the number of exclamation points I used in my slides!?
- recombine letters, but change vowels into numbers for a variant of "L33T SP34K"
Looping Over Each Character
We can iterate over every character that's in a string.
- we've used a construct that lets us iterate over every element in an ordered sequence
- what was that construct?→
For Loops and Strings
- for loops allow us to iterate over every char in a string.
- similar to looping over a sequence of #'s
- variable specified in loop represents the current char
- the loop continues until there are no letters left
- what would the above code print out? →
Write a Program That Iterates Over a String
Try implementing this program:
- use a for loop to go over every letter in "jetpack"
- for each letter, print out the letter with a suffix of "am" appended it
Letter in Word
Try implementing this function! (there's something that's built-in to Python that does this, and we'll see it later)
- implement a function called letter_in_word
- it should take two arguments, a letter and a word
- it should return True if the letter is in the word, otherwise, return False
- use assert on a True case and on a False case
Slicing
You can also retrieve a substring from another string.
- you can get a section of consecutive characters from a string
- example: "ana" is a substring in the string "banana"
This is done using slicing. Slicing syntax works as follows:
Slicing Syntax
Looking at the slicing code again:
The general case is:
- m is the start index and n is the end index.
- the resulting substring starts at m, and goes up to, but does not include n
Substring Exercises
Write the slice to pick out the following substring from the original string:
- hi
- bob!
- bob
Some Slicing Tricks
- leaving out the first index (before the colon - m) starts at the beginning of the string
- leaving out the second index (after the colon - n), ends at the end of the string
- if the second index, if n is bigger than the length of the string, up to the end is sliced
An Easier Way to Tell if a Letter is in a Word
in and not in or operators that each take two arguments. They will test the membership of an element in a collection/sequence.
- in the case of strings, that's character in word
- in
- returns True if element on left is in element in right
- False otherwise
- not in
- returns True if element on left is not in element in right
- False otherwise
In / Not In Examples
Some Potential Exercises
- is_digit
- accepts a string
- returns True if every character in a string is numeric (0 through 9)
- hint: use in and or not in… and the string "0123456789"
- remove_vowels
- accepts a string
- returns a string without any vowels in it
- hint: build a string by accumulating characters