Himanshu SharmaSoftware engineer
Let’s talk

Python Tutorial · Lesson 22 of 70

Python string indexing and slicing explained

Pick single characters with indexes, cut out parts of a string with slices, use negative positions and steps, and reverse text with [::-1].

13 minWorking with textUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonRead any character or section of a string using indexes and slices, including negative positions and step values.

After this lesson you can pull a single character or any section out of a string, such as the first three letters of a code, the file extension at the end of a name, or a string written backwards.

Every character has a position

A string is an ordered sequence, so each character sits at a numbered position called an index. Counting starts at zero, not one. Put the index in square brackets straight after the string.

python
word = "library"
print(word[0])
print(word[1])
print(word[6])
result
l
i
y

The word has seven characters, so valid indexes run from 0 to 6. Asking for position 7 is an error:

python
word = "library"
print(word[7])
result
IndexError: string index out of range

A way to picture it: the index is how many characters you skip from the start. To get the first character you skip none, so you ask for [0].

Counting from the end

Negative indexes count backwards. -1 is the last character, -2 the one before it and so on. This saves you from working out the length when you want something at the end.

python
word = "library"
print(word[-1])
print(word[-3])
print(word[len(word) - 1])
result
y
a
y

The last line is the long way of writing word[-1]. Negative indexes are almost always nicer.

Slicing with start and stop

A slice takes a section. Write two numbers separated by a colon: text[start:stop]. It begins at start and stops just before stop. The stop position is not included.

python
code = "PY-2026-FINAL"
print(code[0:2])
print(code[3:7])
print(code[3:])
print(code[:2])
print(code[-5:])
result
PY
2026
2026-FINAL
PY
FINAL

Study the middle lines. Leaving out start means "from the beginning" and leaving out stop means "to the end". The last line takes the final five characters.

The "up to but not including" rule has a useful side effect. The length of a slice is stop - start, so code[3:7] has four characters. Another is that code[:3] + code[3:] always rebuilds the original string, with no overlap and no gap.

python
code = "PY-2026-FINAL"
print(code[:3] + code[3:])
print(code[:] == code)
result
PY-2026-FINAL
True

Using a step

A slice may take a third number, the step. It says how far to move between characters. text[start:stop:step] takes every step-th character.

python
alphabet = "abcdefghij"
print(alphabet[::2])
print(alphabet[1::2])
print(alphabet[2:8:3])
result
acegi
bdfhj
cf

A negative step walks backwards. With nothing else specified, [::-1] runs through the whole string from the end to the start, which gives you the reverse.

python
word = "library"
print(word[::-1])
result
yrarbil

A classic use is checking for palindromes, words that read the same in both directions. A word is a palindrome when it equals its own reverse:

python
for word in ("level", "library"):
    print(word, word == word[::-1])
result
level True
library False

Slices forgive, indexes do not

A single index outside the string raises an IndexError, as you saw above. A slice that runs past the ends does not complain. It gives you whatever part exists.

python
word = "cat"
print(word[1:100])
print(word[10:20])
print(repr(word[2:1]))
result
at

''

This makes slices safe when the length is unknown. name[:10] gives at most the first ten characters, and does not fail for a short name.

Slices make new strings

A slice never changes the string it came from. It builds a new string, which is how the immutability rule from earlier lessons works out in practice. To change one character, take the part before it, the new character and the part after.

python
word = "cat"
word = "b" + word[1:]
print(word)
result
bat

Where slicing is used

Slices appear constantly in everyday code. You will take the first few characters of a long title to make a preview, cut the extension off a file name, pull the year out of a fixed-format date such as 2026-11-14, or drop a newline from the end of a line. The same square-bracket and colon notation also works on lists and tuples, which you will meet in the collections section, so learning it once pays off several times. If a slice ever gives a surprising answer, write the positions of each character beneath the string and mark where start and stop fall. The mistake is almost always an off-by-one in the stop value.

Common mistakes

  • Starting to count at one. The first character is index 0.
  • Expecting the stop position to be included. "python"[1:3] is "yt", two characters.
  • Using an index when you need a slice. text[5] is one character, while text[5:] is the rest of the string.
  • Going one past the end with a single index. For a string of length n, the last valid index is n - 1, or -1.
  • Assigning to an index. word[0] = "b" is a TypeError because strings are immutable.

A video version of this topic is in the video course at /learn/python/.

Exercise

A product code looks like "BK-0457-HB": a two-letter category, a four-digit number and a two-letter format. Using slices only, print the category, the number and the format on separate lines, then print the code reversed.

Show a possible answer
python
code = "BK-0457-HB"
print(code[:2])
print(code[3:7])
print(code[-2:])
print(code[::-1])
result
BK
0457
HB
BH-7540-KB

Key points

  • Every character has an index. The first is 0, and negative indexes count back from the end, so -1 is the last character.
  • text[start:stop] returns the characters from start up to, but not including, stop.
  • Leaving out start or stop means the beginning or the end. text[:] is a full copy.
  • A third number is the step. text[::2] takes every second character and text[::-1] reverses the string.
  • An index outside the string raises an IndexError, but a slice never raises an error. It simply returns less.

Check yourself

1. What is 'python'[0]?
2. What is 'python'[-1]?
3. What is 'python'[1:4]?
4. What does 'python'[::-1] return?
5. What does 'cat'[10:20] return?