String Indexing

Strings are an incredibly useful component of python programming. We can use strings to store all kinds of information within our programs. Peoples’ names, street addresses, paragraphs of text, ID numbers, and more. Strings have many built-in features that allow us do do things such as extract sub-components, extract individual letters, capitalize words, etc. Later on in this book, there will be a whole mini-series of lessons on how to use strings in more advanced ways. For this lesson, we will focus on one specific feature that strings have, which is called String Indexing.

When dealing with strings, we can refer to any letter / character within a string by its index. An index is just a number that specified what position a character is at within the string. This is akin to various other numberings that we use in everyday life. For example, we use a basic numbering system to specify floors of a building. One can say “I work on the 8th floor of the Gould Simpson building” or “I live on the 3rd floor of the apartment”. In a similar way, we can ask python to give us the character at the 8th position, or at the 3rd position, of a string. There is one key difference to keep in mind though. When we talk about indexes of a string, we always start counting from 0, rather than 1, which is what most will be used to.

For example, say we have this string:

city = 'Jerusalem'

The indexes of this string can be visualized like so:

Index 0 1 2 3 4 5 6 7 8
Character J e r u s a l e m

We can say things like “The letter ‘J’ is at index 0” or “The letter ‘r’ is at index 2”. Notice that this is off-by-one from what you’d typically be used to, since we start the counting from 0. Counting from 0 rather than 1 is a common occurrence in computer programming. In fact, indexes will come up again later in this book when we talk about lists, and again in that case, index counting will start from zero. So get used to it!

So whats is the point of all of this? Well, we can use these indexes to “grab” individual characters from strings to inspect, analyze, or build new strings from them. To do this, we use indexing and the bracket symbols ( [] ) to specify which character we want. For example, the following code:

city = 'Jerusalem'
letter = city[4]
print(letter)

Will print out s because that’s the character at index 4. You can also build new strings by extracting characters from another string.

city = 'Jerusalem.'
rock = city[0] + city[1] + city[8]
print(rock)
What does this program print out when run?
er.
Incorrect. Recall that string indexes begin at 0, not 1.
Jem
Correct! 'J' is at index 0, 'e' is at index 1, and 'm' is at index 8.
Jam
Incorrect. One of those letters is wrong
meJ
Incorrect. You have it backwards!

This would print out Jem because we extracted ‘J’, ‘e’, and ‘m’ from ‘Jerusalem’, concatenated them together into one string, and printed that string out. You can also use negative numbers as string indexes. Negative numbers provide a way to index relative to the end of the string, rather than the beginning. For the ‘Jerusalem’ string the negative indexes would be:

Index -9 -8 -7 -6 -5 -4 -3 -2 -1
Character J e r u s a l e m

Notice how these “start” from -1 rather than -0, because -0 and 0 are really both just 0. To get ‘Jem’ using negative indexes instead we could do:

city = 'Jerusalem'
rock = city[-9] + city[-8] + city[-1]
print(rock)

Negative indexes are convenient if you know you want to get characters relative to where your string ends, but are not sure how long the string is. This could happen, for instance, if you have a string as user input which can be of various lengths.

Say we have a program where we want to print out the last 2 letters of whatever the user writes as input, no matter how long. We can easily accomplish this with negative indexes:

text = input('Type something: ')
print('The last two characters of your input were: ' + text[-2] + text[-1])

This could also be done without negative indexes, but it would be slightly trickier and would require an additional feature. If we were using positive indexes, we would have to have a way to know what the length of a string is. This can be done in python using the len() function. To get the indexes of the second-last and last characters, we would need to use the indexes length-2 and length-1. In code, this would be:

text = input('Type something: ')
last = text[len(text) - 1]
other = text[len(text) - 2]
print('The last two characters of your input were: ' + other + last)

For now, string indexing might not seem like a super useful or practical feature of python programming, but it definitely is. This topic will return again later in this book when you learn about loops, so stay tuned.



PyFlo Home Complete Bookmark Next