VOOZH about

URL: https://thenewstack.io/how-to-use-the-python-slice-function/

⇱ How to Use the Python slice() Function - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2023-12-17 04:00:46
How to Use the Python slice() Function
Python / Software Development / Tech Culture

How to Use the Python slice() Function

Python uses the slice() function to index sections of a sequence.
Dec 17th, 2023 4:00am by Jack Wallen
👁 Featued image for: How to Use the Python slice() Function
Feature image via Unsplash.

When writing Python programs, you will often make use of a range of items (such as numbers, letters, etc.). For example, you might use the numbers 1 through 10 in a list, and throughout your application, you might need to refer to different sections of that list. In one instance, you might only want to refer to the first two characters. In another section, you might want to reference the range from indexes 2 to 6.

Here’s an example of a list:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

With Python, indexing is the processing of accessing elements in a sequence. These elements can be a single number, a string of numbers, a single word, a string of words, or a combination of different types. Each number (word, etc.) is an index, so 1 is index 0, and 10 is index 9 (because Python, like most programming languages, starts counting at 0 and not 1).

Indexing is an important concept because it is so often used.

To index sections of a sequence, Python uses the slice() function. With this function, you can specify where to start the slicing and where to end it. So, in our example above, we can tell Python to start the slice at index 2 (which would be 3) and end at index 7 (which would be 8). If we do this, our slice would be:

3, 4, 5, 6, 7, 8

Or, we could have a list of colors, like:

[red, blue, green, yellow, purple, pink, black, orange]

We could slice that sequence from 1-4, which would be:

blue, green, yellow, purple

The syntax for a slice looks like this:

slice(start, end, step)

Ah, we haven’t talked about the step yet.

By default, the slice() function steps by 1. So our above slicing works out. But you can specify the step you need. For example, if you step by 2 in our above list, the results would be:

blue, yellow

But why does it only include two elements? Remember, our slice went from 1-4 and steps by 2. Since yellow is our second element (which is index 4), the next jump (by a step of 2) would be 5, which is outside of our slice range, so it stops at 3 (yellow). If we started our slice at 0 and went to 4 (stepping by 2), the results would be:

red, green, purple

Starting to make sense now?

Okay, let’s see a slice in action. We’ll first create a slice example with numbers. Since slice() is a part of the built-in Python functions, we don’t have to call any outside libraries, so we can immediately start by defining my_list like so:

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Next, we’re going to use the slice() function to define x as a slice object that goes from 2 to 7 with:

x = slice(2, 7)

Finally, we use the print() function to print out our slice with:

print(my_slice[x])

What we’ve done above is use our defined slice variable (x) with our defined list (my_list). Create the new script with the command:

nano slice.py

Our full script looks like this (I’ll add documentation for demonstration):

Save and close the file.

Run the script with the command:

python3 slice.py

The output will be:

[3, 4, 5, 6, 7]

Let’s create a similar script, that’s a bit more complicated. Create this with:

nano slice2.py

Add the following into the file:

Save and close the file.

Run the new script with:

python3 slice2.py

The output should be the same as when you ran the slice.py script (so [3, 4, 5, 6, 7]).

Now, let’s use the step function in our first script. We’ll create the slice, stepping by 2. Go back to the slice.py script and change:

x = slice(2,7)

to:

x = slice(2,7,2)

Save and close the file. The new script should look like this:

Run the script with:

python3 slice.py

The output should be:

[3, 5, 7]

And that, my Python-loving friends, is how you use the slice() function.

TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.