Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only.Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Reversing a String Using a Slice
00:00In 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:10What exactly does this mean?The termimmutable describes an object whose state or value cannot be changedafter it has been created.This means that every modification to an object will result in a new object.
00:23This has interesting performance implications that will pop up later in thecourse.The termsequence 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:41Sequence types implement the.__getitem__() magic method, commonly used to access itemsgiven 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:10Slices are a common way of defining a subset of items you wish to access byindex. Slices have the optional argumentsstart,stop,andstep, similar to therange() built-in function.
01:21These arguments are usually of typeint.Thestart andstop arguments are usually used to denote the beginning and theend of the subsequence you wish to index.
01:31Thestep argument then defines the fixed interval at which the items areindexed. Ifstart is not provided,it’s set to0, referring to the index of the first element of your sequenceobject. Similarly, ifstop is not provided,it is set to the final element of your sequence. Ifstep is not provided,it defaults to1, meaning every element betweenstart andstop is indexed.
01:56Ifstep is negative, the slice will operate on your sequence in reverse.
02:02The.__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:13The bracket operator has a similar syntax of the slice, with the argumentsstart,stop, andstep. These behave identically to the slice.
02:22For example, let’s create a string comprised of the characters"Hello World!"and assign it to the variablegreeting. Let’s also definestart,stop,andstep to be0,1, and1, respectively.
02:35You can now use either the bracket operator syntax or.__getitem__() with a slicedirectly. You should get the same result.
02:44Since the argumentsstart,stop, andstep are optional,you can experiment with not specifying them and seeing what the output is.
02:51For example, using the same string as before,you can try passing astart of0,stop of5, and astep of1,which should index the first five characters with an interval of one—in this case,"Hello". Sincestep is by default1,you don’t have to specify it, and you can simply write[0:5].
03:10Recall thatstart also defaults to0,so it doesn’t have to be specified either. You can just write[:5].
03:18Finally, 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:34Recall that thestep argument specifies the interval at which items areindexed.Astep equal to2 would index every second element betweenstart andstop.
03:45step has a special behavior for negative values. When the values are negative,step operates on the stringin reverse order, indexing every nth element betweenstop andstart backwards,where n is the value ofstep. Settingthestep to1 would select all the elements betweenstart andstop, as we’veseen before.
04:02Setting it to-1 does the exact same but in reverse order.Therefore,using astep of-1 will select all characters betweenstart andstop inreverse. Ifstart andstop are left to their respective defaults,the entire sequence is selected in reverse.
04:21This allows us to reverse sequences such as strings using either the bracketoperator or the.__getitem__() method withstart andstop left blank and astep of-1.
Course Contents

