Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

String Indexing

Strings are ordered sequences of character data. Indexing allows you to access individual characters in a string directly by using a numeric value. String indexing is zero-based: the first character in the string has index 0, the next is 1, and so on. In this lesson, you’ll learn string indexing syntax and practice with several examples:

>>> s = 'mybacon'
>>> s[0]
>>> s[1]
>>> s[6]
>>> s[len(s) - 1]

>>> s[7]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
IndexError: string index out of range

Here’s some negative string indexing:

>>> s
>>> s[-1]
>>> s[-4]
>>> s[-len(s)]
>>> s[-7]
>>> s[-8]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
IndexError: string index out of range

>>> t = ''
>>> type(t)
<class 'str'>
>>> t[0]
Traceback (most recent call last):
  File "<input>", line 1, in <module>
IndexError: string index out of range
>>> len(t)

00:00 Often in programming languages, individual items in an ordered set of data can be accessed directly using a numerical index or a key value. This process is referred to as indexing. Strings are ordered sequences of character data,

00:15 and the individual characters of a string can be accessed directly using that numerical index. String indexing in Python is zero-based, so the very first character in the string would have an index of 0,

00:30 and the next would be 1, and so on. The index of the last character will be the length of the string minus one. Let’s look at an example. For the string 'mybacon', the first index would be for the letter 'm', 0, and so forth.

00:48 And the last one, even though it’s seven characters in the string, the last index would be the number 6 for the letter 'n'. Let’s try this out interactively. Create a string, and I’ll use the same one that was in the slides.

01:01 The string s will be 'mybacon'. To access the first item in my string I’ll use square brackets, in this case with index 0, and that will pull up my first letter from my string.

01:14 So s[0] is 'm', and so forth. Index 1 would be the second character, index 5 would be the second to last one, and the last character would be index 6. Even though it’s the seventh character, that’s the last one.

01:30 Another way to get to that would be to use the built-in function len(), so the length of the string s minus 1—that should get you your last character also. If I were to try to go to an index that’s beyond the length of the string, say 7, it’s going to give an error.

01:47 It’ll say the string index is out of range. In the same way, if you use length just by itself, you’d get the same thing. The index is out of range. So that’s why you need to remove one to get to that last item.

01:58 String indices can also be specified with negative numbers. Let’s look at that in the slides. Negative indexing is also available. In this case, you’d just count backwards.

02:07 -1 would be coming from the back, so it would be the letter 'n' at the end of the string. Versus -7, which would be your first letter. Let me have you try that out. Again, we’re dealing with the string s, 'mybacon'.

02:20 So if s[-1] in your square brackets—that would provide the last letter. And so forth, -4.

02:29 And again, the function len(s) is that it’s 7 characters. So what if you were to say s and negative len(s)? Well, that’d be your first letter, 'm', just as if I had typed -7.

02:44 What happens if you give it too high of a value or too—in this case, negative number—too low of a value? Again, it’s out of the range. If you were to have an empty string, let’s call it t. t is a <class 'str'>, but it’s empty.

02:59 If you were to try to access the first index, you’d also get a range error, because it’s out of the range. There is no length to this string,

03:10 so you’d get an error. Something pretty neat that you can do with these indexes is not only access individual characters of the string, but you can also grab ranges of that.

03:21 It’s called string slicing, and that’s up next.

Become a Member to join the conversation.