Skip to content

Slice

In one line

A slice extracts a portion of a sequence using [start:stop:step] syntax.

In simple words

A slice extracts a sub-sequence from a list, string, tuple, or other sequence. The syntax is sequence[start:stop:step]:

  • start — the beginning index (inclusive, default 0)
  • stop — the ending index (exclusive, default len)
  • step — the stride (default 1)

All three are optional. s[:] copies the whole sequence. s[::-1] reverses it. Negative indices count from the end. Slicing always returns a new object — it never modifies the original.

Slicing is one of Python's most powerful features. It works on any sequence type and is very efficient for strings and lists.

Example

python
s = "Hello, World!"
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

print(s[0:5])     # Hello
print(s[7:])      # World!
print(s[:5])      # Hello
print(s[::-1])    # !dlroW ,olleH (reversed)

print(nums[2:8])     # [2, 3, 4, 5, 6, 7]
print(nums[::2])     # [0, 2, 4, 6, 8] (every other)
print(nums[::-1])    # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Slicing with [start:stop:step] extracts subsequences. Negative indices count from the end; step controls the stride.

Common confusions

  • Confused with: index

    The difference: An index (s[i]) accesses a single element. A slice (s[i:j]) extracts a sub-sequence. Indexing returns one item; slicing returns a new sequence of items.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary