Back to Basics - Python #03

Photo by Laura Ockel on Unsplash

Back to Basics - Python #03

String slicing

Better than sliced bread

String slicing allows us to extract specific portions of a string by specifying a start and an end indices. We can even skip characters! How about that! For beginners, slicing things using a programming language can be a confusing experience, to say the least. There are so many new things to remember: learning to count like a computer, that we must start at index 0 (zero), and keeping in mind that the end index is not included, i.e., the last character we originally thought would be returned, is not the character being returned. I beg your pardon! They all add to an initial sense of frustration. That frustration shall pass. Hopefully sooner than later.

In this article, we will explore various scenarios with examples.

The Basics of String Slicing

1. Slicing from the Start

We can obtain a substring by specifying the start index and the end index (not included) using the slice syntax. The first character in the string has an index of 0 (zero). Here's an example:

s = "Ice cream"
# Get characters from position 2 to 4 (not including 5)
substring = s[2:5]  
print(substring)  # Output: "e c"

Have you noticed that the space between 'ice' and 'cream' was also counted? Empty spaces are counted as normal characters when we use the slice method.

If we omit the start index, the default start index is 0 (zero), the range will start from the beginning of the string:

# Get characters from the start to position 4 (not including 5)
substring = s[:5]  
print(substring)  # Output: "Ice c"

2. Slicing to the End

Leaving out the end index will extend the range to the end of the string. The default end index is -1 (minus 1):

# Get characters from position 2 to the end
substring = s[2:]  
print(substring)  # Output: "e cream"

3. Negative Indexing

We can also use negative indexes to slice from the end of the string. For example:

# Get characters from "o" (position -5) to "d" (position -2)
substring = s[-5:-2]  
print(substring)  # Output: "cre"

Advanced Slicing Techniques

1. Skipping Characters

To skip characters while slicing, use a step value. The syntax is start:end:step. For instance:

s = "Python is amazing!"
# Get characters from position 1 to 9, skipping every second character
substring = s[1:10:2]  
print(substring)  # Output: "yhni"

2. Reverse Slicing

To reverse a string, use a negative step value:

# Get the entire string in reverse
reversed_string = s[::-1]  
print(reversed_string)  # Output: "!gnizama si nohtyP"

That read like a Harry Potter spell, right?

Summary

String slicing is a powerful tool in Python for manipulating and extracting substrings. Remember the basics: specify the start and end indices, and optionally include a step value for more advanced slicing.

Try not to get too frustrated if, initially, your code does not return what you originally intended; it happens to all of us, all the time. Persistence and, sometimes a good night of sleep, are all that will take to get things right.

Wait!?! Why do I, all of a sudden, fancy a piece of toast?

Happy coding!

References:

  1. W3Schools¹
  2. Real Python²
  3. Enterprise DNA³
  4. llego.dev
  5. GeeksforGeeks