GoalFind a character by its position in a string, use len() to count characters, and avoid positions that do not exist.
New words
index · the number of a position in a string or list; the first position is 0len() · a function that gives the number of characters in a stringIndexError · the error when a program asks for a position that does not exist
Explanation
Each character in a string has a position number, called its index, and the first one is index 0. To read a character, write the string followed by the index in square brackets, such as word[3]. The function len(word) gives the number of characters, and spaces and symbols count too. Because counting starts at 0, the last index is len(word) - 1. You can use that expression directly, as in word[len(word) - 1], to get the last character. If you ask for an index that does not exist, Python stops with an IndexError. Only strings can be indexed, so a number such as 256 must first be cast with str().
The word Notebook has eight characters, so the indexes run from 0 to 7. word[0] is N and word[3] is e. The last character is word[len(word) - 1], which is word[7], the letter k. The length is 8. Each line was run.
A ticket code made from a city name: the first letter, followed by the number of letters. For Cairo that is C and 5. The number must be cast with str() so that it can be joined to the letter. For Tashkent the code would be T8.
Python
1word = "Notebook"2print(word[8])
Output
IndexError: string index out of range
A position that does not exist. The word has 8 characters, so the last index is 7 and word[8] is one past the end. Python stops with an IndexError. Also remember that spaces count: len("sun and sea") is 11, because there are 9 letters and 2 spaces.
Class activity
Letter boxes (no computer). Write a word of 8 or 9 letters on a strip of paper, with one letter in each box, and write the index above each box starting at 0. Your partner calls out instructions such as word[2], word[len(word) - 1] or word[9]. You point to the box and say the letter, or say ‘IndexError’ if there is no box. Then count a sentence including its spaces and check the length by writing a number under every character.
Practice
1
The program starts with word = "Sailboat". Complete the table.
Expression
Value
word[0]
?
word[4]
?
word[7]
?
len(word)
?
word[len(word) - 1]
?
word[0] is S, word[4] is b, word[7] is t, len(word) is 8, and word[len(word) - 1] is t.
AL and then 8. first[0] is A and second[0] is L, so they are joined to give AL. The lengths 4 and 4 add up to 8.
3
Python stops with an error on line 2. Which error, and why? Correct the program so that it prints the last letter.
Python
1word = "tree"2print(word[4])
IndexError: string index out of range. The word tree has four characters, so the last index is 3 and word[4] does not exist. Use word[3] or word[len(word) - 1].
4
Write a program that asks for a word and prints the first character, the last character and the length.
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): word = input("Word? ") / print(word[0]) / print(word[len(word) - 1]) / print(len(word))