Words and sentences are fundamental to how we communicate, so it follows that we’d want our computers to be able to work with words and sentences as well.
In Python, the way we store something like a word, a sentence, or even a whole paragraph is as a string. A string is a sequence of characters. It can be any length and can contain any letters, numbers, symbols, and spaces.
In this lesson, you will learn more about strings and how they are treated in Python. You will learn how to slice strings, select specific characters from strings, search strings for characters, iterate through strings, and use strings in conditional statements.
String as Lists
A string can be thought of as a list of characters.
Like any other list, each character in a string has an index. Consider the string
|favorite_fruit = “blueberry”|
We can select specific letters from this string using the index. Let’s look at the first letter of the string.
Whoops, is that the first letter you expected? Notice that the letter at index 1 of “blueberry” isn’t b, it’s l. This is because the indices of a string start at 0. b is located at the zero index and we could select it using:
It’s important to note that indices of strings must be integers. If you were to try to select a non-integer index we would get a TypeError:
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: string indices must be integers, not float
Cut Me a Slice of String
Not only can we select a single character from a string, we can select entire chunks of characters from a string. We can do this with the following syntax:
This is called slicing a string. When we slice a string we are creating a new string that starts at (and includes) the first_index and ends at (but excludes) the last_index. Let’s look at some examples of this. Recall our favorite fruit:
Let’s say we wanted a new string that contains the letters eberr. We could slice favorite_fruit as follows:
Notice how the character at the first index, e, is INCLUDED, but the character at the last index, y, is EXCLUDED. If you look for the indices 3 and 8 in the diagram, you can see how the y is outside that range.
We can also have open-ended selections. If we remove the first index, the slice starts at the beginning of the string and if we remove the second index the slice continues to the end of the string.
Again, notice how the b from berry is excluded from the first example and included in the second example.
You can also concatenate two existing strings together into a new string. Consider the following two strings.
|fruit_prefix = “blue”
fruit_suffix = “berries”
We can create a new string by concatenating them together as follows:
|favorite_fruit = fruit_prefix + fruit_suffix
Notice that there are no spaces added here. You have to manually add in the spaces when concatenating strings if you want to include them.
|fruit_sentence = “My favorite fruit is “ + favorite_fruit
‘My favorite fruit is blueberries.`
Python comes with some built-in functions for working with strings. One of the most commonly used of these functions is len(). len() returns the number of characters in a string
|>>> favorite_fruit = “blueberry”
If you are taking the length of a sentence the spaces are counted as well.
|>>> fruit_sentence = “I love blueberries”
len() comes in handy when we are trying to select the last character in a string. You can try to run the following code:
|>>> length = len(favorite_fruit)
But this code would generate an IndexError because, remember, the indices start at 0, so the final character in a string has the index of len(string_name) – 1.
You could also slice the last several characters of a string using len():
Using a len() statement as the starting index and omitting the final index lets you slice n characters from the end of a string where n is the amount you subtract from len().
In the previous exercise, we used len() to get a slice of characters at the end of a string.
There’s a much easier way to do this, we can use negative indices! Negative indices count backward from the end of the string, so string_name[-1] is the last character of the string, string_name[-2] is the second last character of the string, etc.
Here are some examples:
|>>> favorite_fruit = ‘blueberry`
Notice that we are able to slice the last three characters of ‘blueberry’ by having a starting index of -3 and omitting a final index
Strings are Immutable
So far in this lesson, we’ve been selecting characters from strings, slicing strings, and concatenating strings. Each time we perform one of these operations we are creating an entirely new string.
This is because strings are immutable. This means that we cannot change a string once it is created. We can use it to create other strings, but we cannot change the string itself.
This property, generally, is known as mutability. Data types that are mutable can be changed, and data types, like strings, that are immutable cannot be changed.
Occasionally when working with strings, you’ll find that you want to include characters that already have a special meaning in python. For example let’s say I create the string
|favorite_fruit_conversation = “He said, “blueberries are my favorite!“”|
We’ll have accidentally ended the string before we wanted to by including the “character. The way we can do this is by introducing escape characters. By adding a backslash in front of the special character we want to escape, \”, we can include it in a string.
|favorite_fruit_conversation = “He said, \”blueberries are my favorite!\””|
Now it works!
Iterating through Strings
Now you know enough about strings that we can start doing the really fun stuff!
Because strings are lists, that means we can iterate through a string using for or while loops. This opens up a whole range of possibilities of ways we can manipulate and analyze strings. Let’s take a look at an example.
for letter in word:
This code will iterate through each letter in a given word and will print it to the terminal.
|>>> favorite_color = “blue”
Let’s try a couple problems where we need to iterate through a string.
Strings and Conditionals
Now that we are iterating through strings, we can really explore the potential of strings. When we iterate through a string we do something with each character. By including conditional statements inside of these iterations, we can start to do some really cool stuff.
Take a look at the following code:
|favorite_fruit = “blueberry”
counter = 0
for character in favorite_fruit:
if character == “b”:
counter = counter + 1
This code will count the number of bs in the string “blueberry” (hint: it’s two). Let’s take a moment and break down what exactly this code is doing.
First, we define our string, favorite_fruit, and a variable called counter, which we set equal to zero. Then the for loop will iterate through each character in favorite_fruitand compare it to the letter b.
Each time a character equals b the code will increase the variable counter by one. Then, once all characters have been checked, the code will print the counter, telling us how many bs were in “blueberry”. This is a great example of how iterating through a string can be used to solve a specific application, in this case counting a certain letter in a word.
There’s an even easier way than iterating through the entire string to determine if a character is in a string. We can do this type of check more efficiently using in. in checks if one string is part of another string.
Here is what the syntax of in looks like:
|letter in word|
Here, letter in word is a boolean expression that is True if the string letter is in the string word. Here are some examples:
|>>>“e” in “blueberry”
>>> “a” in “blueberry”
In fact, this method is more powerful than the function you wrote in the last exercise because it works not only with letters, but with entire strings as well.
|>>> “blue” in “blueberry”
>>> “blue” in “strawberry”
Great work! I hope you are now starting to see the potential of strings and how they can be used to solve a huge variety of problems.
In this lesson you learned:
- A string is a list of characters.
- A character can be selected from a string using its index string_name[index]. These indices start at 0.
- A ‘slice’ can be selected from a string. These can be between two indices or can be open-ended, selecting all of the string from a point.
- Strings can be concatenated to make larger strings.
- len() can be used to determine the number of characters in a string.
- Strings can be iterated through using for loops.
- Iterating through strings opens up a huge potential for applications, especially when combined with conditional statements.
Let’s put your new skills to the test!