Please enable JavaScript to use CodeHS

Slicing Python Strings

By Ryan Molyneaux

Slicing Strings

In the Indexing Strings for Python tutorial, we reviewed how to access data placed in the form of strings. Because we know how to locate by indexing, it would be beneficial to delve deeper and explore how to manipulate the data using indices.

One of the most helpful python functions to use for strings is the slice( ) function. This function allows the program to access one or more characters in sequential order. The programmer can specify the beginning and end points by using the following syntax:
slice(start, end, step)

Start and End

When the slice function is initiated, Python will automatically default the start position to zero unless it is told otherwise. Because of this, start is optional. The next parameter is end, which should be the index value of where you want the program to stop slicing. Note, that the program will end counting just before the index indicated.

Take a look at the code below to see how using either 0 as a parameter or leaving it blank will yield the same result.


If you look at the output for both functions, you’ll see that you have the same result. This is because the program will assume it should start slicing at the zeroth index if no start parameter is indicated.

You’ll also notice that each function only prints "alpha", "beta", "gamma" because the slicing stops at the index before the end parameter. So, while the index of “delta” is 3, it will not print.

Step

The step parameter is used to tell the program how many places to count before slicing again. This is optional and can be helpful when you have a set number of places that you want to skip in a list.

In the example below, let’s say you want the program to count and print every other pastel color, starting at the zeroth index.

Check Your Understanding

In this tutorial, we took a closer look at how to access specific items in a list using the slice ( ) function. We know how to use the start, end, and step parameters to tell the program what and how to access certain strings within a list. Now that you are aware of the basics, take a few minutes to solidify your comprehension by reviewing the Vocabulary List and completing the Challenge Scenario below.


Vocabulary Terms

Review the following terms and try to define on your own.

  • Start Parameter
  • End Parameter
  • Step Parameter

Challenge Scenarios

Review the program below and complete the following prompts.

  1. List 1: Imagine you are a Performing Arts Director that needs to write an article on the most relevant actors of 2020. The following program contains a list of artists in the following pattern: Vocal Artist, Actor, and Director. Review the tuple named, `actors`, and fill in the correct parameters needed to print the names of actors. Your output should resemble the following:
    ("Lupita Nyong'o", 'Michael B. Jordan', 'Daniel Kaluuya', 'Zendaya', 'Tom Holland', 'Millie Bobby Brown')
    Plain text
  2. List 2: Alter the tuple named, `second_half` so that only the second half of all the artists will print on the screen. Your output should resemble the following:
    ('Brendon Urie', 'Zendaya', 'Kenya Barrris', 'Lady Gaga', 'Tom Holland', 'Steven Spielberg', 'Jack White', 'Millie Bobby Brown', 'Ava DuVernay')
    Plain text
  3. List 3: Beyoncé has acquired the title of fashion designer while Ava DuVernay has taken on the new role of screenwriter. Because their statuses have changed, we need to remove them from the list. Alter the tuple named, `bookends` so that Beyoncé and Ava DuVernay are removed from the entire list. Your output should resemble the following:
    ('Lupita Nyong'o', 'Judd Apatow', 'Post Malone', 'Michael B. Jordan', 'Tim Burton', 'Shawn Mendes', 'Daniel Kaluuya', 'George Lucas', 'Brendon Urie', 'Zendaya', 'Kenya Barrris', 'Lady Gaga', 'Tom Holland', 'Steven Spielberg', 'Jack White', 'Millie Bobby Brown')
    Plain text