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.
word = "library"
print(word[0])
print(word[1])
print(word[6])l
i
yThe word has seven characters, so valid indexes run from 0 to 6. Asking for position 7 is an error:
word = "library"
print(word[7])IndexError: string index out of rangeA 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.
word = "library"
print(word[-1])
print(word[-3])
print(word[len(word) - 1])y
a
yThe 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.
code = "PY-2026-FINAL"
print(code[0:2])
print(code[3:7])
print(code[3:])
print(code[:2])
print(code[-5:])PY
2026
2026-FINAL
PY
FINALStudy 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.
code = "PY-2026-FINAL"
print(code[:3] + code[3:])
print(code[:] == code)PY-2026-FINAL
TrueUsing 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.
alphabet = "abcdefghij"
print(alphabet[::2])
print(alphabet[1::2])
print(alphabet[2:8:3])acegi
bdfhj
cfA 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.
word = "library"
print(word[::-1])yrarbilA 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:
for word in ("level", "library"):
print(word, word == word[::-1])level True
library FalseSlices 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.
word = "cat"
print(word[1:100])
print(word[10:20])
print(repr(word[2:1]))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.
word = "cat"
word = "b" + word[1:]
print(word)batWhere 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, whiletext[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 aTypeErrorbecause 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
code = "BK-0457-HB"
print(code[:2])
print(code[3:7])
print(code[-2:])
print(code[::-1])BK
0457
HB
BH-7540-KBKey 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.