This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

The Samurai Programmer


Slicing is pulling a section out of a text string and only displaying the section. For example let’s say the string “abcdefghijklmnopqrstuvwxyz” is in the variable ‘alphabet’. So by typing ‘print alphabet’ we would see the output of the entire alphabet.

But can we just show the two letters of the alphabet I use with my teenage son all the time, by typing ‘print alphabet[13:15]’ ? We sure can.

See what’s happening here is that every string is really a series of characters in an ‘array’, and what we’re doing is calling the 13th to the 15th slice between letters and displaying everything in between.

We can also do everything to it we can do with any other variable, like concatenate:

Here’s a couple examples, feel free to experiment…

Task Discussion