This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 8: Strings as a sequence

Strings are also sequences

face Josiah Wang

A Python string is also a sequence, more specifically a text sequence (of characters).

So you can access individual characters, perform slicing and iterate over strings just as you would do with lists/tuples.

You can also use the +, *, in and not in operators as in a list/tuple.

Like tuples, a str is immutable. So you cannot change a character in a string directly or append() directly to an existing string.

Since everything is the same as tuples, let’s just jump straight into a quiz!

1 2 3 4 5 6 7 8 9 10 11 12 13

Question 1

What is the output of the following code snippet? Type Error if it results in an error.

1
2
song_title = "My Way" 
print(len(song_title))
6
Explanation:

Remember to count the space between "My" and "Way"!

Question 2

What is the output of the following code snippet? Type Error if it results in an error.

1
2
song_title = "My Way" 
print(song_title[3])
W
Explanation:

Don't forget to include the space between "My" and "Way"!

Question 3

What is the output of the following code snippet? Type Error if it results in an error.

1
2
song_title = "My Way" 
print(song_title[-1])
y

Question 4

What is the output of the following code snippet? Type Error if it results in an error.

1
2
song_title = "My Way" 
print(song_title[1:4])
y W

Question 5

What is the output of the following code snippet? Type Error if it results in an error.

1
2
song_title = "My Way" 
print(song_title[-3:])
Way

Question 6

What is the output of the following code snippet? Type Error if it results in an error.

1
2
song_title = "My Way" 
print(song_title[0:6:2])
M a
Explanation:

This will slice out the characters at indices 0, 2, and 4 ("M", " " and "a" respectively).

Question 7

What is the output of the following code snippet? Type Error if it results in an error.

1
2
song_title = "My Way" 
print(song_title[::-1])
yaW yM
Explanation:

The usual Python idiom that reverses a sequence. You should be familiar with this by now!

Question 8

What is the output of the following code snippet? Type Error if it results in an error.

1
2
3
song_title = "My Way" 
song_title[3] = "D"
print(song_title)
Error
Explanation:

Remember that a str is immutable, so you cannot modify a str. Python will output an error message: TypeError: 'str' object does not support item assignment.

Question 9

What is the output of the following code snippet? Type Error if it results in an error.

1
2
3
song_title = "My Way" 
for character in song_title:
    print(character)
M y W a y
Explanation:

You can use a for loop to iterate over the characters of a string (don't forget the space!)

[You can assume that your answer is correct if it looks like what is displayed - the quiz system has problem storing spaces in the answer.]

Question 10

What is the output of the following code snippet? Type Error if it results in an error.

1
2
my_name = "Josiah"
print(my_name + my_name)
JosiahJosiah
Explanation:

Hopefully this is second nature by now!

Question 11

What is the output of the following code snippet? Type Error if it results in an error.

1
2
3
my_name = "Josiah"
my_name += my_name
print(my_name)
JosiahJosiah
Explanation:

The += operator will concatenate my_name and my_name and create a new object "JosiahJosiah", and assign this new object to my_name.

Question 12

What is the output of the following code snippet? Type Error if it results in an error.

1
2
my_name = "Josiah"
print("j" not in my_name)
True
Explanation:

This is straightforward, I hope. It checks if the character "j" does not occur in the string "Josiah". It does not, so it evaluates to True.

Question 13

What is the output of the following code snippet? Type Error if it results in an error.

1
2
punctuations = "?.!,;:"
print("?" in punctuations)
True
Explanation:

This is valid - the string does not have to be a proper word. You could also represent the punctuations as a list i.e. ["?", ".", "!", ...]. Consider string.punctuation in Python's string module if you ever need to do something like this; it will give you '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'.