A string is not just a piece of text. Internally, it's a sequence of characters, and Python lets us access individual characters or extract a part of the string.
This is done using indexing and slicing.
String Indexing
Indexing is used to access a specific character from a string.
Python uses zero-based indexing, which means the first character has index 0.
For example:
word = "Python"The indexes are:
P y t h o n
0 1 2 3 4 5So we can access characters like this:
word = "Python"
print(word[0])
print(word[2])
print(word[5])Output:
P
t
nNegative Indexing
Python also supports negative indexing, which starts from the end of the string.
P y t h o n
-6 -5 -4 -3 -2 -1For example:
word = "Python"
print(word[-1])
print(word[-3])Output:
n
hThis is particularly handy when you want to access characters from the end without knowing the string's length.
String Slicing
Slicing is used to extract a portion of a string.
The basic syntax is:
string[start:end]The start index is included, but the end index is not included.
For example:
word = "Python"
print(word[0:3])Output:
PytWhy? Because indexes 0, 1, and 2 are included, while index 3 is excluded.
P y t h o n
0 1 2 3 4 5
↑ ↑
start endLeaving Out the Start or End
You don't always need to provide both indexes.
From the Beginning
word = "Python"
print(word[:3])Output:
PytWhen the start is omitted, Python starts from the beginning.
To the End
print(word[3:])Output:
honWhen the end is omitted, Python continues until the end.
Copying the Entire String
print(word[:])This returns the complete string.
Using a Step
Slicing also supports a third value called step.
The syntax is:
string[start:end:step]For example:
word = "Python"
print(word[0:6:2])Output:
PtoIt takes every second character.
The indexes selected are:
0 → P
2 → t
4 → oReversing a String
One of the most useful slicing tricks is:
word = "Python"
print(word[::-1])Output:
nohtyPThe -1 step tells Python to move backward through the string.
Slicing with Negative Indexes
You can also use negative indexes while slicing:
word = "Python"
print(word[-4:-1])Output:
thoRemember that the ending index is still excluded.
Indexing vs Slicing
It's easy to confuse the two, so here's the simple difference:
Operation | Example | Result |
|---|---|---|
Indexing |
| One character |
Slicing |
| Multiple characters |
Reverse |
| String in reverse |
Important: Strings Cannot Be Changed
Indexing lets you read a character, but you cannot directly replace it.
This will cause an error:
word = "Python"
word[0] = "J"Strings are immutable, meaning their characters cannot be changed directly.
Instead, you can create a new string:
word = "Python"
word = "J" + word[1:]
print(word)Output:
JythonA Practical Example
Let's say we have a username:
username = "john_smith"
print("First character:", username[0])
print("Last character:", username[-1])
print("First four characters:", username[:4])
print("Username reversed:", username[::-1])Output:
First character: j
Last character: h
First four characters: john
Username reversed: htims_nhojOnce you understand indexing and slicing, working with strings becomes much easier. You'll use them frequently when processing names, usernames, text, files, and user input.