Python: string indexing

The tutor gives a Python example of string indexing.

In computer science, a string is a word which can include non-alphabetical characters. An example of a string is “desk17”.

Let’s imagine creating the variable thestring with the value “desk17”, as follows:

thestring=”desk17″

The characters in “desk17” can be referred to by number. Going forward, the letter d is in position 0. However, the string can be counted backward: 7 is in position -1. Therefore, the program


thestring=”desk17″
print(thestring[0])
print(thestring[-1])

produces the output

d
7

I’ll be talking more about Python strings in future posts:)

Source:

Donaldson, Toby. Python, 3rd Edition. Peachpit Press, 2014.

Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.

Tagged with: , , ,

Leave a Reply