String operations
In Python, both lists and strings are considered types of sequences. That means that many of the operations that work on lists also work on strings.
The len()
function reports how long a string is (which is, in the example below, the length of the longest English word ever!):
illness = 'pneumonoultramicroscopicsilicovolcanoconiosis'
longest_word_record_length = len(illness)
The in
operator checks to see if a substring is inside a larger string:
if 'pneumo' in illness:
print("Uh oh, your poor lungs :(")
When used on strings, a for
loop iterates through each character:
alphabet = 'abcdefghijklmnopqrstuvwxyz'
for letter in alphabet:
print(letter)
Bracket notation
We can use bracket notation to access single characters inside a string:
illness = 'pneumonoultramicroscopicsilicovolcanoconiosis'
true_start_sound = illness[1] # Stores 'n'
last_letter = illness[-1] # Stores 's'
However, strings are different from lists in that they are immutable, meaning they cannot be changed after they're created. Using bracket notation to try to change a character will result in an error.
illness[0] = "n" # 🚫 Error!
We can also use bracket notation to slice parts of the string, from a start to (just before) an end index:
how_small = illness[13:24] # Stores 'microscopic'
from_where = illness[30:37] # Stores 'volcano'
Exercise: Bracket notation
Use this string variable for the next three questions:
author_name = 'P S Fox'
String methods
Python offers many methods for string-specific functionality. Since strings are immutable, however, none of the methods change the original string. They either return a new string entirely, or return some other result like a boolean, number, or a list.
For example, the upper()
method returns an uppercase version of a string.
request = "Please wash the dishes from your cookie disaster."
demand = request.upper()
When that code runs, request
is still "Please wash the dishes from your cookie disaster." but demand
stores "PLEASE WASH THE DISHES FROM YOUR COOKIE DISASTER." (Code based on a true story, but I was the one who caused the disaster.. 🍪)
Here are some of the most helpful string methods:
Method |
Description |
---|---|
|
Returns a copy of the string with all characters converted to lowercase. |
|
Returns a new string where occurrences of old are replaced with new. |
|
Returns the lowest index in the string where substring is found. |
|
Returns True if the string starts with the prefix, and False otherwise. |
|
Returns True if the strings ends with the suffix, and False otherwise. |
|
Returns a copy of the string with leading and trailing characters removed, defaulting to whitespace characters. |
|
Returns a list of substrings in the string, using separator as the delimiter. |
|
Returns a string which is the concatenation of strings in the iterable object. |
More methods are described in the Python documentation.
Remember that these are methods, not functions, so when using them, you always write the variable name first, then the dot, then the method name.
Splitting and joining
There are two string methods that I want to spend more time on, because they're just so dang useful, but can also be a little tricky to use: split
and join
.
split(separator)
: Returns a list of substrings in the string, using separator as the delimiter.join(iterable)
: Returns a string which is the concatenation of strings in the iterable object.
The split
method is handy whenever you have a string of text with parts separated by a particular character, like a comma, colon, pipe, space, or tab.
For example, we can split a string that stores a comma-separated list of words:
my_groceries = 'apples,bananas,carrots'
grocery_items = my_groceries.split(',') # Stores ["apples", "bananas", "carrots"]
Now we can iterate through that list - or anything else we can usually do with a list.
for item in grocery_items:
print(item)
The join
method is the opposite: it joins a list of items into a single string, separating each item with the specified separator.
Here's how we can join together a list using a comma as the separator:
address_parts = ["123 Pining St", "Nibbsville", "OH"]
",".join(address_parts) # '123 Pining St, Nibbsville, OH'
We can also use the empty string as the separator, if we just want to mash strings together:
names = ["Gray", "Fox"]
"".join(names) # 'GrayFox'
Exercise: Write a poem
In order to create a string with items separated by new lines, we can specify the escape sequence "\n" as the separator.
Try it out below with your own favorite poem, haiku, or song verse.
poem_lines = ["Forgive me", "they were delicious", "so sweet", "and so cold"]
"\n".join(poem_lines)