Tsallaka zuwa abun ciki
Justice Tech School

Slicing Strings and Lists

Slicing uses [start:stop]. It includes start but stops before stop. Leave a part out to go to the edge, and use a negative number to count from the end. A third number is the step, and [::-1] reverses the sequence.

Slicing works on strings, lists and tuples and always returns a new object.

Example
word = "Programming"
print(word[0:4])
print(word[3:])
print(word[:3])
print(word[-3:])
print(word[::2])
print(word[::-1])

Result

Prog
gramming
Pro
ing
Pormig
gnimmargorP

Exercise

Get the first three characters.

print("Python"[:3])

Quiz Test

What is "Python"[1:4]?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.