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 →
>>> "hello"[0] = 'c'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>>
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
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
defletter_in_word(letter,word):result=Falseforcinword:ifc==letter:result=TruebreakreturnresultassertTrue==letter_in_word('c',"chihuahua"),"letter is in word"assertFalse==letter_in_word('x',"chihuahua"),"letter is not in word"
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:
>>> "placate"[3:6]
'cat'
Slicing Syntax
Looking at the slicing code again:
>>> "placate"[3:6]
'cat'
The general case is:
some_long_string[m:n]
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:
sentence="hi bob!"# 0123456
hi
bob!
bob
sentence[0:2]sentence[3:7]sentence[3:6]
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
"eggs and ham"[:4]#eggs"eggs and ham"[9:]#ham"eggs and ham"[9:100]#ham
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
>>> 'c' in "cat"
True
>>> 'c' not in "cat"
False
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"