Please enable JavaScript to use CodeHS

Indexing Python Strings

Learn how to access specific items by indexing through Python strings.

By Ryan Molyneaux

Indexing Strings

In the Python Strings tutorial, we reviewed how each string is made up of a finite number of characters. In terms of accessibility, we should look a little deeper into what goes between the quotation marks of a string to see about accessing this data.

Take a moment to look at the following example of a string.


We know that the syntax is fairly straightforward, in that there are several characters in the form of letters and one exclamation point. However, something that isn’t initially obvious is the presence of spaces. As part of the English language, we know that words are to be separated with spaces in order to indicate a small pause before the next word. While you would most likely still be able to decipher what is being said, it may take you a moment to understand what is written.

For example: "Thisisastring!"

We would consider the above example to be mechanically incorrect. To have the program print out the string as a complete sentence, you would need to “speak” Python’s language by including what is known as whitespace. Whitespace is referred to as the blocks or spaces between characters and is created by using the tab, return, or space bar on your keyboard. Despite the presence of ‘emptiness’, this is still counted by Python and most programming languages.


Positive Index Numbers

Because strings are considered a finite sequence of characters, we can deduce that each character must be read in order by the program. Therefore, each character (whitespace included) will be represented by what is known as an index. An index is the location of a particular item within a data set.

When read from left to right, the index count begins with zero and increases by one until the end of the data type. The example, pictured below has a total of 13 characters, including the whitespaces. However, because the index count begins with zero, the number count goes up to 12.

Let’s say that we want to access the “a” in the above string and print it onto the screen. We can do this by using the print function and passing through the index value as a parameter. To do this, we would count from zero and record the number that applies to “a”. In this case, the index is 4. Take a look at the example below.


issa_string = “I’m a string!”
print (issa_string[4])

Output: a
Plain text

Negative Index Numbers

There is another method for accessing a character, which can come in handy for accessing characters that are placed toward the end of a lengthy string. When this circumstance occurs, you would read from right to left, starting with -1, instead of zero.

To access the same letter, “a” we would use -9 instead.

issa_string = “I’m a string!”
print (issa_string[-9])

Output: a
Plain text

Now, since “a” is closer to the center of the string, using a negative index to access it wouldn’t necessarily make things easier. However, accessing the punctuation mark using this method would! We can see this in action by using one of Python’s string methods, known as strip( ).

In the example below, we will create a new variable named drab and tell the program to print an unexciting version of the string, minus the exclamation point.

issa_string = "I'm a string!"
drab = issa_string.strip(issa_string[-1])
print (drab)
Plain text

Check Your Understanding

To recap, we know that because strings are comprised of a finite number of characters in sequential order, each block of whitespace must be accounted for. Python is able to access each character and block of whitespace by assigning what is known as an index to each of these items. We have discussed how to count indices using both positive and negative numbers and print the appropriate item onto the screen. To check your understanding, take a look at the Vocabulary List to see if you can define each term on your own. For extra practice, you can run the program at the end of this tutorial. See if you can create your own code using what you’ve learned.


Vocabulary Terms

  • String
  • Whitespace
  • Index


Try It Out!