All courses › Programming with Python › Strings and indexing

Strings and indexing

A string is a sequence of characters. Each character has an index starting at 0, and negative indices count backwards from −1-1. A slice s[i:j] gets the characters from index ii up to and including j−1j-1.

s[i]s[i]the character at index ii
s[i:j]s[i:j]slice from ii up to and including j−1j-1
s[−1]s[-1]the last character

Symbols

ssa string (or list)
i, ji,\ jstart and end index

Example

s = "robotikk"

s[2:6] gives "boti" (indices 2, 3, 4, 5).

s[-1] gives "k".

The length of s[i:j] is j−ij - i — the last index is never included.
Practise python basics for free →

← Numbers and types · Lists →

Part of Programming with Python: Python basics.