AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Data in detail

Week 7 · 1.5 Programming with data · 3 lessons
2

Positions in a word

Computing 9: Topic 1.5, pp. 89–118
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().

Index01234567
CharacterNotebook
Worked examples
Python
1word = "Notebook"
2print(word[0])
3print(word[3])
4print(word[len(word) - 1])
5print(len(word))
Output
N
e
k
8
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.
Python
1city = input("City? ")
2code = city[0] + str(len(city))
3print(code)
Output
C5
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.
ExpressionValue
word[0]?
word[4]?
word[7]?
len(word)?
word[len(word) - 1]?
2
Write down what this program prints.
Python
1first = "Amir"
2second = "Lola"
3print(first[0] + second[0])
4print(len(first) + len(second))
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])
4
Write a program that asks for a word and prints the first character, the last character and the length.