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
stroperations and slicing to compute a new string representing the desired text.Apply
listoperations to store, retrieve, and modify values in a list.
We’ll be writing doctests to verify that our programs work.
import doctestString 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]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.
s = "hello world"
s[len(s) - 2]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
forloop and building-up the result by adding each character one at a time.
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]To slice all the way to the end of a string, simply don’t specify an end position.
s = "hello world"
s[2:]Slices also allow a third parameter, step size, that works just like in range.
s = "hello world"
s[2:8:2]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])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)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.
"I really like dogs".find("ll")If the string s2 is not found in s1, the function returns -1.
"ll".find("I really like dogs")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"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 ofss.upper()returns a new string that is the uppercase version ofss.find(t)returns the index of the first occurrence oftins. 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 ofssplit up according to thedelim(defaults to whitespace).s.join(strings)returns a single string consisting of the givenstringswith the stringsinserted between each string.
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()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.
words = "I really like dogs".split()
words[2] = "love"
wordsPractice: 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):
"""
TODO: Your docstring here
>>> count_votes([1, 0, 1, 1, 2, 0])
[2, 3, 1]
"""
...
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)addsxto the end ofl.l.extend(xs)adds all elements inxsto the end ofl.l.insert(i, x)insertsxat indexiinl.l.remove(x)removes the firstxfound inl.l.pop(i)removes the element at indexiinl.l.clear()removes all values froml.l.reverse()reverses the order of all elements inl.l.sort()rearranges all elements oflinto sorted order.
words = "I really like dogs".split()
"dogs" in words