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.
00:14 Concatenation joins two strings together, indexing gets a single character from a string, and slicing gets several characters from a string at once.
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.
The indexes start from
0 and go up to
8. So to access the first character, you would use index
0. Let’s say you wanted the fifth character. Well, that would be index
4, and so forth.
03:04 Let’s try this out in the interactive window.
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
If you try to index beyond the range—let’s say you use
9—it will tell you that. It’ll say the string index is out of range in
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
04:22 Negative indices may not seem useful at first, but sometimes they are a better choice than a positive index. Let me have you check that out in the interactive window.
So for negative indices, such as the last character in the string, you can start with
-1. And to get that first character, that would be
What if you go beyond? Again, you get the
out of range. This may not seem useful at first, but we’ll come up with some handy things for using negative indices here in a second.
One way that you could try to access the last character of a string might be to use the
len() function. You might have some user input as a string.
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
last_character would be
user_input[final_index]. Again, you could see that this might be much easier to do with just having
-1. It’s going to take a lot less typing.
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.
06:29 That’s where slicing comes in.
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.
So, in the case of
[x:y], it starts at the character at index
x and goes right up to, but does not include, the index at
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
"fig pie", the slice of
[0:3] returns the string
"fig", and the slice
[3:7] would return the string of a space and the word
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
If you went from index
3 up to index
7, that would return
" pie". An interesting thing happens if you go over the number.
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.
It’s important to note that unlike with string indexing, Python won’t raise an
IndexError when you try to slice between boundaries that fall outside the beginning or the ending of a string.
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.
09:07 It’s just doesn’t have any characters in it. It’s different from one that has a space in it.
09:15 There you can see the one space. Or let’s say you had, like, several spaces in there.
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.
Now there’s some interesting things that you can do. Like you can see here, how would you capture all of that string of
"fig pie"? Well, there’s a couple ways around it.
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?
Would you go from
0, you know, because
-1 is going to get up to the
"pie", but not include the
"e". So would you use
Well, no. That returns an empty string, and the reason for that is, if you might remember,
0 is already used as an index.
0 is the first character.
10:47 So there is a way around this. It’s to omit the second index. And that means to actually go to the end of the string.
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.
Doing a slice of
[:5] will return just
If you omit the index after the colon, it’s going to end with the last character. So, you can see here,
dessert[6:] would grab just the word
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
And then the last technique that you learned was that if you have just a
: that will return the whole string.
12:44 So to wrap this lesson up, one last thing that you need to know is that strings are immutable, which means that you can’t change them once they’ve been created.
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.
13:14 You can’t reassign a letter inside there.
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.
13:56 You have to actually do it through a reassignment statement.
It’s time for you to practice what you’ve learned this lesson. Here are a few review exercises to try. create a string and print its length using the built-in
14:13 Try creating a couple strings and concatenate them, and then print the resulting string. Create two strings, use concatenation to add a space in between them, and print out the result.
Print the string
"zing" by using slice notation to specify the correct range of characters in the string
14:38 Next up, it’s time to start manipulating strings with methods.
Become a Member to join the conversation.