Indexing and slicing strings

Python strings functionally operate the same as Python lists, which are basically C arrays (see the Lists section). Unlike C arrays, characters within a string can be accessed both forward and backward. Frontward, a string starts off with a position of 0 and the character desired is found through an offset value (how far to move from the beginning of the string). However, you also can find this character by using a negative offset value from the end of the string. The following screenshot briefly demonstrates this:

String indexing

Line 30 creates a string variable, and then line 31 requests the characters at position ...

Get Learn Programming in Python with Cody Jackson now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.