Strings and Index Numbers


A string of text in Python is series of individual characters. Each individual character is given a number. That number is the character's position in the string. For example, take this string of text and the variable we're placing it into.

myWord = "String"

String is five characters long. The first character, S, gets the number of 0 assigned to it. The second character, t, gets the number 1 assigned to. The letter r is number 2, i is number 3, n is number 4 and g is number 5. Like this:

A string and its index positions

Because we've placed the word String into a variable, you can use the variable name, with something called an index number, to get at each character.

Try this example.

You can delete any code you have so far, except for the code that imports the tkinter message box. Now add the line of code from above:

myWord = "String"

Add another variable and some text:

message = "The letter at position 0 is: "

We can use concatenation to join our new text with the myWord variable. However, if we just did this:

message = "The letter at position 0 is: " + myWord

Then the message would this:

The letter at position 0 is: String

To get at a position in a variable that contains text, you use an index number between square brackets:

message = "The letter at position 0 is: " + myWord[0]

The square bracket come immediately after your variable name.

Now add a message box:

messagebox.showinfo( "Index Numbers", message )

Or this, for older versions of Python:

tkMessageBox.showinfo( "Index Numbers", message )

When you run you code, you should see your message displayed:

Message box showing index position

Now change the zeros in your code to 2s:

message = "The letter at position 2 is: " + myWord[2]

Run your code again and the new message is this:

Message box showing a different index number

You don't have to start at the beginning of your text. You can start at the end by using -1 as the index number:

message = "The last letter is: " + myWord[-1]

The second last letter would then be -2:

message = "The second to last letter is: " + myWord[-2]


Index out of bounds

If you type a number between the square brackets that is larger than the number of characters in the word, you'll get an error. In the line below, we've typed a 7:

message = "The last letter is: " + myWord[7]

When the code is run, the program crashes with this message in the console:

IndexError: string index out of range

So if you see this message, you've typed an index number that is too high.


Python String assignment

You might be wondering if you can change letter in a string by accessing the index numbers. For example, you may have thought about changing the word String to Strong like this:

myWord[3] = "o"

If you try it, you'll get this error:

TypeError: 'str' object does not support item assignment

What this error message is telling you is that can't change letters in your string by assigning a new one in place of the old one. That's because strings are said to be immutable, meaning they can't be changed. You have to create a new string. You'll see how to do that in a later section when do some string manipulation. Strings and index numbers will play a big part. For now, though, we'll move on and explore conditional logic. You'll also learn something more about the message box.

In the next lesson, you'll learn how to get input from a user.

User Input >