Slicing Arrays and Strings Using The Colon Operator In Python

Strings and arrays can be manipulated in a number of ways in python. Splitting a string or an array into sections can be done using the section slicing that is built into the core python syntax and uses the colon (:) symbol.

Generating a simple array in python is straightforward, the following creates an array of 9 elements.

an_array = [1,2,3,4,5,6,7,8,9]

To grab a particular element of the array you can use the simplest section slicing syntax. The number in the square brackets represents the position of the item to be returned. Remember that the counting starts at 0, so using 1 will return the item from the "second" position.

print(an_array[1]) # prints 2

To return a particular section of the array you just add another number to the section slicing syntax using a colon (:). In this case the first number represents the starting item and the second number represents the ending item.

print(an_array[0:3]) # prints [1, 2, 3]
print(an_array[2:3]) # prints [3]
print(an_array[0:9]) # prints [1, 2, 3, 4, 5, 6, 7, 8, 9]

One warning here is if you use the same number for the start and end of the section. In this case you return an empty array.

print(an_array[1:1]) # prints []
print(an_array[3:3]) # prints []
print(an_array[4:4]) # prints []

You can also add a negative number in order to count backwards from the end of the array.

print(an_array[5:-2]) # prints [6, 7]

Be careful when using this technique as if you allow the pointers to cross over you'll just get a blank array.

print(an_array[8:-8]) # prints []

As a time saving measure you can also just miss out the start point of the slice. If you miss out the first pointer then it is assumed that the first pointer is the beginning of the array.

print(an_array[:-7]) # prints [1 , 2]
print(an_array[:2]) # prints [1 , 2]

It is also possible to pass a third item to the section slicing that allows you to controls the step of the returned items. For example, passing '2' will mean that every second item will be returned in the result.

print(an_array[2:8:2]) # prints [3, 5, 7]

We can simplify this by just passing in the third parameter and using the double colon syntax (::). If we pass in '2' as the parameter then we essentially get every odd item in the array (since the count starts at 0). The following two lines are identical since Python assumes that we mean the start and end of the array if we miss out those arguments.

# get every 'odd' item in the array
print(an_array[::2]) # prints [1, 3, 5, 7, 9]
print(an_array[0:9:2]) # prints [1, 3, 5, 7, 9]

If you've ever seen Python syntax with a double colon then it is getting an offset value of the array or string. 

As another example we can use the same technique to get every event number in the array if we start from position 1.

# get every 'even' item in the array
print(an_array[1::2])

To get every 4th element in the array we can just pass in 4 as the third argument.

print(an_array[::4]) # prints [1, 5, 9]

The same technique can also be done with strings, in much the same way as arrays work. Here are some examples of the previous snippets with a string. First, let's define a string with the same number of items in it.

a_string = 'abcdefghi'

If we go through the above examples again we can slide up the string in a number of different ways using the slice syntax.

# Get a single letter.
print(a_string[1]) # prints b

# Get a sequence of letters.
print(a_string[0:3]) # prints abc
print(a_string[2:3]) # prints c
print(a_string[0:9]) # prints abcdefghi

# Get a sequence of letters with a negative offset.
print(a_string[5:-2]) # prints fg

# Get all letters from the start of the string to a pointer.
print(a_string[:-7]) # ab
print(a_string[:2]) # ab

# Get every every second letter between characters 2 and 8.
print(a_string[2:8:2]) # prints ceg

# Get every odd letter.
print(a_string[::2]) # prints acegi
print(a_string[0:9:2]) # prints acegi

# Get every even letter.
print(a_string[1::2]) # prints bdfh

# Get every fourth letter.
print(a_string[::4]) # prints aei

Also, if you ask for something that doesn't make sense then Python will return a blank string. Much like how it returns a blank array.

print(a_string[1:1]) # prints ""
print(a_string[3:3]) # prints ""
print(a_string[4:4]) # prints ""
print(a_string[8:-8]) # prints ""

Slicing strings and arrays in Python is easy thanks to the use of this syntax and as this is built into Python it can be used anywhere.

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
6 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.