4.5. Index Operator: Working with the Characters of a String

The indexing operator (Python uses square brackets to enclose the index) selects a single character from a string. The characters are accessed by their position or index value. For example, in the string shown below, the 14 characters are indexed left to right from postion 0 to position 13.

index values

It is also the case that the positions are named from right to left using negative numbers where -1 is the rightmost index and so on. Note that the character at index 6 (or -8) is the blank character.

The expression school[2] selects the character at index 2 from school, and creates a new string containing just this one character. The variable m refers to the result.

The letter at index zero of "Luther College" is L. So at position [2] we have the letter t.

If you want the zero-eth letter of a string, you just put 0, or any expression with the value 0, in the brackets. Give it a try.

The expression in brackets is called an index. An index specifies a member of an ordered collection. In this case the collection of characters in the string. The index indicates which character you want. It can be any integer expression so long as it evaluates to a valid index value.

Note that indexing returns a string — Python has no special type for a single character. It is just a string of length 1.

4.5.1. Index Operator: Accessing Elements of a List

The syntax for accessing the elements of a list is the same as the syntax for accessing the characters of a string. We use the index operator [] . The expression inside the brackets specifies the index. Remember that the indices start at 0. Any integer expression can be used as an index and as with strings, negative index values will locate items from the right instead of from the left.

When we say the first, third or nth character of a sequence, we generally mean counting the usual way, starting with 1. The nth character and the character AT INDEX n are different then: The nth character is at index n-1. The fourth character in a string is at index 3. The seventh element in a list is at index 6. Make sure you are clear on what you mean!

Try to predict what will be printed out by the following code, and then run it to check your prediction. (Actually, it’s a good idea to always do that with the code examples. You will learn much more if you force yourself to make a prediction before you see the output.)

Check your understanding

Assign the value of the 34th element of lst to the variable output.

Assign the value of the 23rd element of l to the variable checking.

Assign the value of the last chacter of lst to the variable output. Do this so that the length of lst doesn’t matter.


Why does counting start at 0 going from left to right, but at -1 going from right to left? Well, indexing starting at 0 has a long history in computer science having to do with some low-level implementation details that we won’t go into. For indexing from right to left, it might seem natural to do the analgous thing and start at -0. Unfortunately, -0 is the same as 0, so s[-0] can’t be the last item. Remember we said that programming languages are formal languages where details matter and everything is taken literally?

You have attempted of activities on this page