 In this video we are going to see that string objects are just sequences in the abstract sense. In an earlier video we learned that there are concrete data types in Python, like the in data type, the flow data type, but also the string data type, and then there are abstract data types which are really just classifications of the concrete ones. So let's go ahead and create a new file and call it strings are sequences because that's what they are. So let's remember what are the typical sequence properties. So let's briefly review that. Sequence properties. There are four of them. The first three of them basically make them a so-called collection, and the fourth one then makes them a sequence. So let's look at them. So the first property is the so-called container property. So let's make an example. So let's go above here, and let's make an example. Let's create a text object and assign it the string lorem iptum do lore sit amid, which is a sentence that a lot of you have probably heard before, the typical example text case, so to say. And now let's see what does it mean for this text object here to be a container. But quite simple. We said that the characteristic operation for a container property is the inner operator just like that, and the inner operator takes on the left-hand side some object, for example, the letter a as a string object, and on the right-hand side it also takes in this case a string object, and we can this way check if the letter uppercase a is included in the text, and the answer of course is false. Similarly, if I go ahead and check if the lowercase a is in text, then the answer would be true. So previously when we used the inner operator, we used it with lists. On the right-hand side we had a list object. However, the inner operator also works for string objects. So strings and lists are two different concrete data types. However, they are both in the abstract sense containers. So that's just one property. What is another one? Another one, another property is usually called the sized property or the finiteness. So in other words, the container contains a finite number of other objects, and we can show that by using the built-in length function. So any data type that is a sized data type supports the built-in length function. So let's try that with text here as the argument, and we get back 27. 27 is the number of characters in the text object here. Okay, so the second property of a sequence is also true. It's also fulfilled by the string data type. So let's look at the third one. The third one is what we call the iterable property. So this is basically the property that tells us that we can loop over an object. And in many previous videos we always looped over the list object. However, we could also go ahead and we could say we loop over the text object. So let's maybe say for x in text, print x, and let's do that. And we see that we get each letter printed on one line at a time. So in other words, what we are looping over, if we want to use a better name for that, would simply be character, okay? So let's maybe do that here. And let's also maybe end the line here with an empty space instead of a new line character. And now we see we print out Lorem Ipsum and so on with a space in between or maybe with two spaces in between. Okay, so text objects, string objects are iterable. So these three properties, container, size, and iterable, whenever they apply to an object simultaneously, we call it a collection, generically speaking. Okay, and all collections that also have a force property and we call that property formally, the so-called reversible property. So let's simply go ahead and copy-paste our for loop example. And let's maybe simply go ahead and use the built-in reversed function. And we can loop over the text in reverse order, okay? And so any object that can be used as an argument to the reversed built-in is what we call a reversible. And now if any object that fulfills all these four properties is what we call a sequence, okay? And sequences we have seen before are list objects. So list objects are one example, one concrete data type that is a sequence in the abstract sense. And the string object or the string data type, the concrete string data type, is also an example of the abstract sequence data type, okay? So now what do these four properties basically enable us to do? So whenever these four properties are enabled or are there, what we can do with an object is we can do what it's called indexing. So we have seen that a couple of times before when we indexed into a list object using the indexing operator. So let's go ahead and index into the text object here. So I use the indexing operator, the brackets, and let's say I want to get the first index, so index 0. Well, this simply gives me the letter L, okay? So that's it. Now let's go ahead and do that further, go further. So now what do the four properties have to do with the indexing operator here? Well, you need to have an order, right? You need to be reversible. You need to have an order. Reversibility means there is also a forward order. Without an order, we could not index because if I want to obtain element number 0 or element number 1, or in this case, character number 0 or character number 1, how could I do that if the characters are not in an order, okay? But here, because the text objects or string objects are reversible, we know that there is an order and therefore we can index. So that is why the indexing operator only works with objects that are reversible. Now, one thing that we have not seen about the indexing operator in this course yet, but we will do so now is we can also index with negative numbers. So let's index with 2-1. And what that does is it basically gives us the last character, okay? So if we go, if we index from left to right, indexes start at 0. If we index from right to left, then indices start at negative 1. So negative 1 simply means the first character going from the right. Now, we know that the text object has 27 characters in it. So in other words, if I obtain index number negative 27, this will also be L, right? So that is what we can do with indexing. And so why is this important? Well, what does that have to do with the properties above here? Well, if the object were not size, so if it were not finite, then we could not do that. Okay, we can only index from right to left, so in a backwards fashion using negative indices. If we know that the object itself has some end, it has a finite number of things in it. And therefore, the sized property is very important because without it, we couldn't use negative indices. Okay, now you see, now you get an idea of why it is very important to understand that we can classify different objects or objects of different data types with these abstract behaviors. And again, those four together are the sequence one. This is a very common one. And this basically enables us the indexing operator. Let's do a generalization of indexing, which is called slicing. So indexing means we get a single character out of the object. And slicing simply means we get a whole slice out of it. So let's try to get the first three letters. So let's go ahead and use the indexing operator. And instead of giving it a single index, we give it a range and lower end and an upper end from where to where we want to slice out something from the text object here. So let's say we want to begin on the left hand side with zero. Now we write the colon. The colon is just syntax. And let's say I want to obtain the first three characters. I want to go from zero to actually three here in this case. So what we see here is the first index, the left index is included. The upper index, the three is excluded. Otherwise, we would get four letters, right? Because three minus four, these are actually four indices. Zero, one, two, three, four, or zero, one, two, three, these are four indices. But we only get three letters. So that means it must be that the upper index is excluded here. Just as we know it from the range built in before. So that is slicing. And let's look at some of the more details about slicing. So whenever I want to take a slice that starts at the beginning, I could simply leave out the left index. So this going from simply colon three is the same as zero colon three here. So when the first index should be zero, we can just leave it away. Also, let's say I want to index from, let's say, the fifth character, which would be index four, until the end. And we know that the length of the text object is 27. So if I go to 27, because the upper index, as we learned, is excluded. So that will go until the end, basically. So we see that we have M something, and then it goes until the included period here. And whenever we go until the end of a slice, what we can do is the converse of what we did here with leaving away the zero. Well, we couldn't simply go ahead and say we go from index four until the end. We get back the same slice here. Okay, so this is slicing, and there is one more thing you need to know about slicing. This is also very much comparable to the range built in, in that we can also use a step size. So let's say I want to get every other character. What I could do is I could simply say colon colon two. And that basically means go from the very first index or from index zero to excluding the index 27 in steps of two. This will give me every other letter here. And again, because we are going from beginning to end, I can just leave it away and say colon colon two, step size of two. If you want to reverse the letters inside a string, this is actually helpful for one of the future exercises. Actually, one of the exercises regarding chapter six. Then what you could do is you could say, colon colon negative one. A step size of negative one will give you the reverse order. Okay, so this is basically the sentence reversed. Now, in the context of textual data, the step size may not be super important. However, everything I taught you regarding the slicing and regarding the indexing not only holds true for strings, but for sequences in general. So also everything I showed you here regarding the slicing also works for lists. Okay, if you want to get only a subset of the elements inside the list, you can do slicing with the list object just as well. Okay, so now there's one more thing that I want to talk about here. So in the example before in previous videos where we talked about the list object. There was one video which I titled, Who Am I and How Many? Where I go ahead and I have a list of numbers and I exchange one of the numbers. And the other numbers stay the same. Okay, and that was important because objects that could be changed after they were created are called immutable objects, okay? And the opposite of immutable object is immutable objects. And the string data type is an example of immutable object. So that's maybe right here immutability. So immutability means if I take the text object like that and let's say I index into that, let's say the first index, the very first character. And let's say I want to exchange that into let's say X uppercase X. So I want to replace the L with an X, I get an error, type error. Because a text object, so a string object, after it is created in memory, it cannot be changed. That's a very important observation, okay? So we just have to live with that. So some objects, some data types are mutable and some are immutable. And the string data type is simply an immutable data type. And in one of the next videos when we talk about text data, we will talk about string methods and we will see that all the string methods, they return a new string object out of an existing one. And that is different from the list method. So the list method often they change a list object in place. But the string methods, they will always give us back a new string, a new object in memory that has to do with mutability versus immutability. And therefore, this concept is rather important. So note that string objects are simply immutable. So I will see you in the next video when we talk about string methods.