← Lesson
Index and Slice
Joeven
Run
Reset
Python loads on first run
word = "hello" print("index 0:", word[0]) print("index -1:", word[-1]) print("slice 1:4:", word[1:4]) print("copy of word:", word[:]) nums = ["a", "b", "c", "d", "e"] print("list slice 1:4:", nums[1:4]) print("every second:", nums[0:5:2]) print("last three:", nums[-3:]) print("past the end:", nums[9:12]) original = [1, 2, 3] same = original copy = original[:] same.append(99) print("original changed:", original) print("copy unchanged:", copy) text = "abcdefgh" print("good chunks:", text[0:4], text[4:8]) print("too short:", text[0:3], text[3:6], text[6:9])
Run to execute this in your browser. Nothing is sent to a server.