Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Reversing a String Using a Slice

00:00 In this lesson, you’ll learn how to reverse a string using slicing and indexing. So what is a string? The Python documentation describes strings as an immutable text sequence type.

00:10 What exactly does this mean? The term immutable describes an object whose state or value cannot be changed after it has been created. This means that every modification to an object will result in a new object.

00:23 This has interesting performance implications that will pop up later in the course. The term sequence refers an ordered collection of elements—in this case, characters. Therefore, in plain English, a string is described as an unchanging ordered collection of characters.

00:41 Sequence types implement the .__getitem__() magic method, commonly used to access items given an index or a key. Remember that sequences refer to an ordered collection of elements.

00:51 .__getitem__() is the recommended way to access those elements by index or key. Magic methods are not usually called directly. In the case of the .__getitem__() method, the square bracket operator ([]) is usually used.

01:04 .__getitem__() accepts either an integer or a slice.

01:10 Slices are a common way of defining a subset of items you wish to access by index. Slices have the optional arguments start, stop, and step, similar to the range() built-in function.

01:21 These arguments are usually of type int. The start and stop arguments are usually used to denote the beginning and the end of the subsequence you wish to index.

01:31 The step argument then defines the fixed interval at which the items are indexed. If start is not provided, it’s set to 0, referring to the index of the first element of your sequence object. Similarly, if stop is not provided, it is set to the final element of your sequence. If step is not provided, it defaults to 1, meaning every element between start and stop is indexed.

01:56 If step is negative, the slice will operate on your sequence in reverse.

02:02 The .__getitem__() method accepts either an integer or a slice. However, as mentioned earlier, it is rare to call .__getitem__() directly, but rather more common to use the bracket operator.

02:13 The bracket operator has a similar syntax of the slice, with the arguments start, stop, and step. These behave identically to the slice.

02:22 For example, let’s create a string comprised of the characters "Hello World!" and assign it to the variable greeting. Let’s also define start, stop, and step to be 0, 1, and 1, respectively.

02:35 You can now use either the bracket operator syntax or .__getitem__() with a slice directly. You should get the same result.

02:44 Since the arguments start, stop, and step are optional, you can experiment with not specifying them and seeing what the output is.

02:51 For example, using the same string as before, you can try passing a start of 0, stop of 5, and a step of 1, which should index the first five characters with an interval of one—in this case, "Hello". Since step is by default 1, you don’t have to specify it, and you can simply write [0:5].

03:10 Recall that start also defaults to 0, so it doesn’t have to be specified either. You can just write [:5].

03:18 Finally, just to show that all the arguments are optional, if you just pass [::] to the bracket operator, the entire string is returned. I recommend experimenting with different sequence types such as lists, as well as different arguments to the bracket operator.

03:34 Recall that the step argument specifies the interval at which items are indexed. A step equal to 2 would index every second element between start and stop.

03:45 step has a special behavior for negative values. When the values are negative, step operates on the string in reverse order, indexing every nth element between stop and start backwards, where n is the value of step. Setting the step to 1 would select all the elements between start and stop, as we’ve seen before.

04:02 Setting it to -1 does the exact same but in reverse order. Therefore, using a step of -1 will select all characters between start and stop in reverse. If start and stop are left to their respective defaults, the entire sequence is selected in reverse.

04:21 This allows us to reverse sequences such as strings using either the bracket operator or the .__getitem__() method with start and stop left blank and a step of -1.

Become a Member to join the conversation.