Python Reference Library →

Python slice() Function – Create Slice Objects for Lists and Strings

Let’s look at slice() function – create slice objects for lists and strings

  1. Notice

    A slice describes start, stop and step for sequence selection. Stop is excluded; omitted bounds use the sequence’s defaults.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'tea'
part = slice(0, 2)
print(text[part])
Live state

Top-level scope

text→Object 1
part→Object 2
Object 1 · str'tea'Object 2 · sliceslice(0, 2, None)
This step

A slice describes start, stop and step for sequence selection. Stop is excluded; omitted bounds use the sequence’s defaults.

Picture slice() function – create slice objects for lists and strings

A slice describes start, stop and step for sequence selection. Stop is excluded; omitted bounds use the sequence’s defaults.

Image brief

Alt text
A diagram for Python slice() Function – Create Slice Objects for Lists and Strings. Instructions: text = 'tea' part = slice(0, 2) print(text[part]). Displayed result: te. The final example bindings are text --> object 1 (str); part --> object 2 (slice). object 1: 'tea'; object 2: slice(0, 2, None)
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python slice() Function – Create Slice Objects for Lists and Strings". Teaching focus: A slice describes start, stop and step for sequence selection. Stop is excluded; omitted bounds use the sequence’s defaults. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'tea' part = slice(0, 2) print(text[part]) | v DISPLAYED RESULT te FINAL BINDINGS text --> object 1 (str) part --> object 2 (slice) object 1: 'tea' object 2: slice(0, 2, None) Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python slice() Function – Create Slice Objects for Lists and Strings. Instructions: text = 'tea' part = slice(0, 2) print(text[part]). Displayed result: te. The final example bindings are text --> object 1 (str); part --> object 2 (slice). object 1: 'tea'; object 2: slice(0, 2, None)

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'tea'
part = slice(0, 2)
print(text[part])
Live state

Top-level scope

text→Object 1
part→Object 2
Object 1 · str'tea'Object 2 · sliceslice(0, 2, None)
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
te

What do you expect this program to print?

What does this example display?

text = 'tea'
part = slice(0, 2)
print(text[part])
What does this example display?
Choose one answer. You can try again.

Answer and explanation

te

Follow the printed values in order. The output is: te

More examples and reference

Python slice() Function

The slice() function in Python creates a slice object that can be used to extract parts of a sequence like a list, tuple, or string. This is very useful when you want to get a sub-part of a sequence using custom start, stop, and step values.

Syntax

slice(stop)
slice(start, stop[, step])

Parameters:

  • start – (Optional) The starting index of the slice. Default is 0.
  • stop – The ending index (exclusive).
  • step – (Optional) The step size or interval. Default is 1.

Returns:

  • A slice object that can be used to extract portions from a sequence.

Example 1: Basic Usage with a List

numbers = [10, 20, 30, 40, 50, 60]
s = slice(1, 4)
print(numbers[s])
[20, 30, 40]

Example 2: Using Step with slice()

letters = ['a', 'b', 'c', 'd', 'e', 'f']
s = slice(1, 6, 2)
print(letters[s])
['b', 'd', 'f']

Example 3: slice() with Strings

text = "HelloWorld"
s = slice(0, 5)
print(text[s])
Hello

How is slice() Different from [start:stop:step]?

Python allows slicing using both slice() and the shorthand notation [start:stop:step]. They do the same thing but slice() is more flexible when you want to pass slicing as a parameter to functions or reuse slicing logic.

Use Case: Reusable Slice Logic

def get_middle_part(seq):
    s = slice(1, -1)
    return seq[s]

print(get_middle_part("Python"))
print(get_middle_part([10, 20, 30, 40]))
ytho
[20, 30]

Common Mistakes

  • Forgetting that the stop index is exclusive.
  • Passing strings instead of integers to slice().
  • Assuming slice() extracts the values itself — it only creates a slice object.

Interview Tip

Use slice() when writing reusable logic or custom sequence functions. Knowing how to slice efficiently is helpful in coding rounds involving arrays or strings.

Summary

  • slice() returns a slice object for use with sequences.
  • You can specify start, stop, and step.
  • Works with lists, tuples, strings, and other slicable types.

Practice Problem

Write a function that takes a list and returns every third element starting from index 0 using slice().

def every_third_element(lst):
    s = slice(0, len(lst), 3)
    return lst[s]

print(every_third_element([1, 2, 3, 4, 5, 6, 7, 8, 9]))
[1, 4, 7]

What to remember

Connect what you noticed in the example with the idea below.

Remember

A slice describes start, stop and step for sequence selection. Stop is excluded; omitted bounds use the sequence’s defaults.

Watch out for this idea

The stop index is included in a slice.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →