Concatenating, Indexing, and Slicing
00:00 In the previous lesson, you learned all about creating strings and writing out string literals. In this lesson, you’re going to learn about concatenation, indexing, and slicing, three basic string operations.
Let’s start with concatenation. To combine, or concatenate, strings, you use the
+ (plus) operator. You’re adding one string to another, and that concatenates them. In this example,
"cadabra", are tied together, or concatenated, into
magic_string by assigning
string2. Let’s try out some examples in the interactive window.
Okay. Here in the interactive window, create the two strings by assigning
"abra" to one and
"cadabra" to the other, and then create a third variable,
magic_string and say
+ string2. That will concatenate them together into
abracadabra. Great. Let’s feed it something with names.
Say you had a
"Arthur" and a
"Dent", and you wanted to create a
full_name, though, needs a space in the middle of it. Well, you can do that through concatenation also.
You would add the
first_name to a string that has a single space (
" ") in it and then also concatenate that with
last_name. So here, we’re adding
" ", and
last_name. That should create the
Now I’m going to have you look at indexing. Each character in a string has a numbered position. It’s called an index. Individual characters can be accessed by using the index. You place the index number inside of a pair of square brackets (
) after the string, something like this.
This would be index
3 and return that character. What may seem a little different is the index count starts with
0. Most programming languages do this, and it is really common to have what’s called an off-by-one error because the thought in your head might be, oh, the counting should start with
1, whereas most often, it actually starts with
In the case of my string with index
3, that would be the fourth position. Let me show what this looks like. Let’s say you had the string
"apple pie" with a space in between. That string is made up of nine characters.
If you were to use an index of
1, that won’t return the first character. It returns the second. The first character—again, because everything is a
0 index—You’d have to use
0. And this whole string is nine characters, so to grab the last one, it would be
8. To get the space in the middle would be
Strings also have negative indices. The last character in a string has an index of
-1. So in the case of
"apple pie", that would be the
"apple pie". To grab the second to last character, you would enter in
-2, and so on.
Just like with positive indices, Python raises an
IndexError if you try to access a negative index less than the index of the first character of the string—in this case,
-9—if you go lower than that, say something like
Let’s just say somebody had typed
"Hello World" into your system, and you wanted to grab that
"d" or that last character. You could say, okay, well, the final index is equal to the length of
user_input, so find the length of the string and then subtract
1 from it. You know, give me that value. So that in this case, for
What if you had a string, like you did before, of
"apple pie", and you wanted to return a string that was just the flavor of the pie, just the word
"apple"? Well, this is one way you could do it, but you can see this might be a bit of a pain. We got there, but that’s a lot of indexing and concatenation.
You can extract a portion of a string, called a substring, by inserting a colon (
:) in between two index numbers set inside the square brackets like this. So in this case, let’s say you wanted to return just the flavor of the pie.
You could put
dessert[0:5], and that will capture just the word
"apple". A slice starts with the character at the first index and goes up to, but does not include, the character at the second index.
In fact, it kind of behaves a little bit more like boundaries around the characters. So, let’s say we had a fig pie. You could see the boundaries starting there, where if you wanted to capture the flavor of that pie, a fig, you would have
0 up to
So let’s make a new dessert, a
"fig pie". So again, if you wanted to return just the flavor of the new dessert, you could go from index
0 up to index
3, and that would return the slice of just
Let’s say you went to
[3:9]. Even though that’s beyond that, it’s okay. It doesn’t create the same kind of error that you got before, where if you went over the index, it told you that it was out of range.
So even if you went and tried to start at an index beyond that—let’s say you went from
12—it simply returns what’s known as an empty string. An empty string, again, is a string that has no characters in it. It’s still a string.
Negative slicing works by the same rules. Again, a slice starts at
x and includes that character and goes up to, but does not include, the character at index
y. So in the case of
-7 up to
-4 would capture that first word.
For negative indexing, let’s say you started at
-7, which would be all the way at the very first boundary, and you went up to
-4. That would return, again, just fig the slice of
"fig". Now, you might be thinking, well, how do you go all the way up to the end?
11:00 So there’s three different ways that you can omit an index. If you omit the first index, it will start with the very first character of the string from the beginning of it and work normally, going up to, but not including, the second index number.
11:36 If you omit both indexes, that returns the whole string, and you can see that in the bottom example. So that’s an option is that you can omit the indexes to get either the beginning of the string or the end of the string, or in the case of removing both indices, you return the whole string.
Okay. I’m going to try a couple of these out in the interactive window, working again with
new_dessert. If you go from
0 and put a
:, it will capture all the way to the end. If you want just the word
"pie", you could start at index
4 and add the
And that would grab just that last word. If you omit the first, again, it will start from the beginning. So if I went from
[:4], that would return
"fig ". Actually, I just wanted the word
"fig", so that would be
Let’s say you assigned
"boil" to the variable
word, and then you went in and tried to change the character that’s at index
0, which is the first letter. And you’re like, okay, let’s reassign the first letter to
"f". You’ll see a
object does not support item assignment.
So, to alter a string, you actually have to create a new one, and that can be done through reassignment. This will actually create a new string object. So, one way to accomplish what we were trying to do a moment ago: I want include the last three letters of
word—in this case, it would be
"oil" to change
"foil"—and concatenate that to the letter
"f". But what’s unique here is you’re reassigning
word, so that’s creating a whole new object—in this case, removing the previous one.
Become a Member to join the conversation.