Strings and Lists¶
In this lesson, we'll introduce strings and lists in Python. We'll also learn the principles of documenting code. By the end of this lesson, students will be able to:
- Evaluate expressions involving strings, string slicing, and lists.
- Apply
str
operations and slicing to compute a new string representing the desired text. - Apply
list
operations to store, retrieve, and modify values in a list.
We'll be writing doctests to verify that our programs work.
import doctest
String indexing¶
Strings are commonly used to represent text. In Python, str
(pronounced "stir") represents a string. We'll refer to string and str
interchangeably since we're focusing on Python programming.
In Python, str
values are defined by surrounding text in matching quotes: either a '
or a "
. The characters in a string are accessible by index starting from 0 and incrementing up to the length of the string.
h | e | l | l | o | w | o | r | l | d | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
To access a character at a specific index, use the s[i]
notation to get a particular character from a string s
.
s = "hello world"
s[0]
'h'
The built-in len
function returns the length of an object such as a string. It helps compute letters from the end of the string.
len(s)
11
s[len(s) - 2]
'l'
s[-2]
'l'
Practice: Pairs swapped¶
Write a function pairs_swapped
that takes a string and returns all the characters in the given string except with each pair of characters swapped. For example, calling the function on a string "hello there"
should produce the result "ehll ohtree"
.
- Start by writing the function definition.
- Add a brief docstring that explains the behavior.
- Add at least two doctests: one using the example above, and another that you came up with on your own.
- Write the method using a
for
loop and building-up the result by adding each character one at a time.
def pairs_swapped(s):
"""
Takes in a string s and returns a new string with each pair of characters in s swapped.
>>> pairs_swapped("cse163")
'sc1e36'
If the input string is of odd length, the last character of the input string will be kept
as is in the returned string.
>>> pairs_swapped("hello there")
'ehll ohtree'
"""
result_s = ""
for i in range(0, len(s) - 1, 2):
result_s += s[i+1]
result_s += s[i]
if len(s) % 2 == 1: # this string is odd length
result_s += s[-1]
return result_s
# doctest.testmod()
doctest.run_docstring_examples(pairs_swapped, globals())
String slicing¶
String indexing gets a single character from a string. How do we get multiple characters from a string? Python has a special syntax called slicing that enables patterned access to substrings: s[start:end]
.
h | e | l | l | o | w | o | r | l | d | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
s = "hello world"
s[2:7]
'llo w'
To slice all the way to the end of a string, simply don't specify an end
position.
s = "hello world"
s[2:]
'llo world'
Slices also allow a third parameter, step size, that works just like in range
.
s = "hello world"
s[2:8:2]
'low'
Looping over strings¶
There are two ways to loop over a string. One way is to loop over all the indices of a string with the help of the range
function.
s = "hello world"
for i in range(len(s)):
print(s[i])
h e l l o w o r l d
Another way is to loop over the characters in a string directly. It turns out that the for
loop in Python iterates over sequences. A range
produces a sequence of integers. A str
is also a sequence composed of the characters within the string.
s = "hello world"
for c in s:
print(c)
h e l l o w o r l d
String functions¶
Strings have convenient utility functions that you can call to answer questions about strings.
For example, every string has a find
function that you can call on a string s1
that returns the index of a given string s2
inside s1
.
s1.find(s2)
"I really like dogs".find("ll")
5
If the string s2
is not found in s1
, the function returns -1.
"ll".find("I really like dogs")
-1
That said, if you only need to check whether s2
is in s1
, Python has a special in
operator for answering this question.
"ll" in "I really like dogs"
True
For future reference, here are some commonly-used string functions. This list is useful to memorize because these functions are used very frequently, but you'll probably learn them over time just by seeing them in other peoples' code.
s.lower()
returns a new string that is the lowercase version ofs
s.upper()
returns a new string that is the uppercase version ofs
s.find(t)
returns the index of the first occurrence oft
ins
. If not found, returns -1.s.strip()
returns a new string that has all the leading and trailing whitespace removed.lstrip()
andrstrip()
remove only left whitespace or right whitespace respectively.)
s.split(delim)
returns a list consisting of the parts ofs
split up according to thedelim
(defaults to whitespace).s.join(strings)
returns a single string consisting of the givenstrings
with the strings
inserted between each string.
sentence = "I really like dogs"
sentence.lower()
'i really like dogs'
sentence.upper()
'I REALLY LIKE DOGS'
sentence.strip()
'I really like dogs'
" print(c)\n\t".strip()
'print(c)'
words = sentence.split()
" ".join(words)
'I really like dogs'
sentence
'I really like dogs'
Lists¶
The s.split(delim)
function defined in the list above introduced another data type called a list. Whereas a string is an indexed sequence of characters, a list is an indexed sequence that can store values of any type.
"I really like dogs".split()
['I', 'really', 'like', 'dogs']
The great thing about lists in Python, is that they share a lot of the same syntax for operations as strings. Concatenation, indexing, slicing, the len
function, and for
looping over a list all works exactly like you learned for strings.
But, there is one major difference between lists and strings.
- Lists are mutable: they allow reassignment of individual values within the list.
- Strings are immutable: the characters within a string can never change. String functions like
s.lower()
return new strings as a result.
sentence = "I really like dogs"
words = sentence.split()
words[2] = "love"
words
['I', 'really', 'love', 'dogs']
Practice: Count votes¶
Write a function count_votes
that takes a list of numbers indicating votes for candidates 0, 1, or 2 and returns a new list of length 3 showing how many counts each candidate got. See the doctest below for one example.
def count_votes(votes):
"""
Takes in a list of votes for candidates 0, 1, or 2. Returns a new list of
length 3 that shows how many votes each candidate got.
>>> count_votes([1, 0, 1, 1, 2, 0])
[2, 3, 1]
>>> count_votes([])
[0, 0, 0]
>>> count_votes([1, 1, 2, 2])
[0, 2, 2]
We do not handle invalid votes that are not 0, 1, or 2.
"""
counts = [0, 0, 0]
for cand in votes:
counts[cand] += 1
return counts
doctest.run_docstring_examples(count_votes, globals())
List functions¶
There are also many list
functions. Lists are mutable, so all these operations modify the given list.
l.append(x)
addsx
to the end ofl
.l.extend(xs)
adds all elements inxs
to the end ofl
.l.insert(i, x)
insertsx
at indexi
inl
.l.remove(x)
removes the firstx
found inl
.l.pop(i)
removes the element at indexi
inl
.l.clear()
removes all values froml
.l.reverse()
reverses the order of all elements inl
.l.sort()
rearranges all elements ofl
into sorted order.
words = "I really like dogs".split()
catwords = "meow meow meow".split()
catwords
['meow', 'meow', 'meow']
words
['I', 'really', 'like', 'dogs']
words.append(catwords)
words
['I', 'really', 'like', 'dogs', ['meow', 'meow', 'meow']]
catwords.insert(1, "purr")
catwords
['meow', 'purr', 'meow', 'meow']
words
['I', 'really', 'like', 'dogs', ['meow', 'purr', 'meow', 'meow']]
Just like we learned how strings support the in
operator, lists also support the in
operator.
print("dogs" in words)
print("dogs" in catwords)
True False
words.append("!")
words
['I', 'really', 'like', 'dogs', '!']
words.extend(catwords)
words
['I', 'really', 'like', 'dogs', '!', 'meow', 'meow', 'meow']
catwords
['meow', 'meow', 'meow']
catwords.insert(1, "purr")
catwords
['meow', 'purr', 'meow', 'meow']
words
['I', 'really', 'like', 'dogs', '!', 'meow', 'meow', 'meow']
catwords.remove("purr")
catwords
['meow', 'meow', 'meow']
catwords.pop()
'meow'
catwords
['meow', 'meow']
words.reverse()
words
['meow', 'meow', 'meow', '!', 'dogs', 'like', 'really', 'I']
# words.sort() # by default, sorts in ascending order
words.sort(reverse=True)
words
['really', 'meow', 'meow', 'meow', 'like', 'dogs', 'I', '!']
words.clear()
words
[]