1a[-1] # last item in the array
2a[-2:] # last two items in the array
3a[:-2] # everything except the last two items
4
1word = "Example"
2
3# Obtain the first 3 characters of "Example"
4# E x a m p l e
5# 0 1 2 3 4 5 6
6# First 3 characters = "Exa"
7sliced_word = word[:3] # Gives you "Exa"
8
9# Everything after character #3 = "mple"
10second_sliced_word = word[3:] # Gives you "mple"
1# array[start:stop:step]
2
3# start = include everything STARTING AT this idx (inclusive)
4# stop = include everything BEFORE this idx (exclusive)
5# step = (can be ommitted) difference between each idx in the sequence
6
7arr = ['a', 'b', 'c', 'd', 'e']
8
9arr[2:] => ['c', 'd', 'e']
10
11arr[:4] => ['a', 'b', 'c', 'd']
12
13arr[2:4] => ['c', 'd']
14
15arr[0:5:2] => ['a', 'c', 'e']
16
17arr[:] => makes copy of arr
1# array[start:stop:step]
2
3# start = include everything STARTING AT this idx (inclusive)
4# stop = include everything BEFORE this idx (exclusive)
5# step = (can be committed) difference between each idx in the sequence
6
7arr = ['a', 'b', 'c', 'd', 'e']
8
9arr[2:] => ['c', 'd', 'e']
10
11arr[:4] => ['a', 'b', 'c', 'd']
12
13arr[2:4] => ['c', 'd']
14
15arr[0:5:2] => ['a', 'c', 'e']
16
17arr[:] => makes copy of arr
1string = 'string_text'
2
3beginning = 0 # at what index the slice should start.
4end = len(string) # at what index the slice should end.
5step = 1 # how many characters the slice should go forward after each letter
6
7new_string = string[beginning:end:step]
8
9# some examples
10a = 0
11b = 3
12c = 1
13
14new_string = string[a:b:c] # will give you: str
15# ____________________________________________
16a = 2
17b = len(string) - 2
18c = 1
19
20new_string = string[a:b:c] # will give you: ring_te
1a[::-1] # all items in the array, reversed
2a[1::-1] # the first two items, reversed
3a[:-3:-1] # the last two items, reversed
4a[-3::-1] # everything except the last two items, reversed
5