Strings as a Compound Data Type

Strings as a Sequence of Characters

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? →

"""+---+---+---+---+---+---+---+---+---+
   | c | o | o | l |   | c | a | t | ! |
   +---+---+---+---+---+---+---+---+---+"""
"""+---+---+---+---+---+---+---+---+---+
   | c | o | o | l |   | c | a | t | ! |
   +---+---+---+---+---+---+---+---+---+
   | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
   +---+---+---+---+---+---+---+---+---+"""

The Last Index

What index is the last character of each of these strings? →

  1. "foo"
  2. "hi"
  3. "!"

The last index is the length of the string - 1

  1. 2
  2. 1
  3. 0

Some More Index Exercises

  1. What's the index of 'h' in "hello"?
  2. What's the index of 'o' in "hello"?
  3. What's the index of the second 'l' in "hello"?
  4. What's the letter at index 0 in "hi there."?
  5. What's the letter at index 2 in "hi there."?
  1. 'h' is index 0 in "hello"
  2. 'o' is index 4 in "hello"
  3. the second 'l' is index 3 in "hello"
  4. 'h' is index 0 in "hi there."
  5. ' ' (space) is index 2 in "hi there"

Indexing Into a String

You can reference a specific character in a string (indexing) by:

a_string[an_index]

Indexing Examples

>>> "hello"[0]
'h'
>>> animal = "aardvark"
>>> animal[4]
'v'
>>> idx = 7
>>> animal[idx]
'k'
>>> 

Indexing Exercises

What does the following code output. Let's walk through it line by line. →

idx = 3
animal = "moose"
print(animal[idx])
print(animal[idx - 3])
print("animal"[1])
print("animal"[5])
s
m
n
l

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. →

idx = -3
animal = "cow"
print(animal[-1])
print(animal[idx])
print(animal[idx + 3])
w
c
c

One Last Note About Indexing

>>> "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:

Looping Over Each Character

We can iterate over every character that's in a string.

#  for loops!
for c in "hello":
  print(c)

For Loops and Strings

my_crazy_string = "oh, my!"
for c in my_crazy_string:
  print(c)
o
h
,
m
y

Write a Program That Iterates Over a String

Try implementing this program:

word = "jetpack"
suffix = "am"
for c in word:
	print(c + suffix)

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)

def letter_in_word(letter, word):
	result = False
	for c in word:
		if c == letter:
			result = True
			break
	return result

assert True == letter_in_word('c', "chihuahua"), "letter is in word"
assert False == letter_in_word('x', "chihuahua"), "letter is not in word"

Slicing

You can also retrieve a substring from another string.

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]

Substring Exercises

Write the slice to pick out the following substring from the original string:

sentence = "hi bob!"
#            0123456
  1. hi
  2. bob!
  3. bob
sentence[0:2]
sentence[3:7]
sentence[3:6]

Some Slicing Tricks

"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 / Not In Examples

>>> 'c' in "cat"
True
>>> 'c' not in "cat"
False

Some Potential Exercises