How slicing in Python works

I’m learning Python and keep seeing slicing used on strings and lists. How exactly does Python slicing work, and how are start, end, and step values applied?

Python slicing is a clean and powerful way to extract parts of strings, lists, tuples, and other sequence types. The basic idea is the same everywhere.


:small_blue_diamond: Basic Syntax

sequence[start : end : step]

  • start → index to begin (inclusive)

  • end → index to stop (exclusive)

  • step → how many positions to move each time

All three are optional.


:small_blue_diamond: Simple Examples

String slicing

text = "Python"

print(text[0:4])   # 'Pyth'
print(text[2:])    # 'thon'
print(text[:3])    # 'Pyt'


List slicing

nums = [10, 20, 30, 40, 50]

print(nums[1:4])   # [20, 30, 40]
print(nums[:3])    # [10, 20, 30]
print(nums[::2])   # [10, 30, 50]


:small_blue_diamond: Step Value (Skipping Elements)

text = "abcdef"

print(text[::2])   # 'ace'
print(text[1::2])  # 'bdf'


:small_blue_diamond: Negative Indexing

Python allows negative indices to count from the end.

text = "Python"

print(text[-3:])   # 'hon'
print(text[:-2])   # 'Pyth'


:small_blue_diamond: Reversing with Slicing

text = "Python"
print(text[::-1])  # 'nohtyP'


:small_blue_diamond: Key Rules to Remember

  • start is included

  • end is excluded

  • Default step is 1

  • Slicing never raises an error if indices are out of range

print(text[0:100])  # Works fine


:small_blue_diamond: Common Use Cases

  • Extract substrings

  • Reverse sequences

  • Skip elements

  • Copy lists safely

copy_list = nums[:]


:white_check_mark: Summary

Slice Meaning
[:] Full copy
[start:] From start to end
[:end] From beginning to end-1
[::step] Skip using step
[::-1] Reverse

If you want, I can share practice questions or visual diagrams to make slicing even clearer :blush:

2 Likes

Slicing in Python is used to extract a part of a sequence such as a string, list, or tuple. It uses the syntax:

sequence[start : end : step]

  • start → index to begin from (inclusive)

  • end → index to stop at (exclusive)

  • step → how many elements to skip (optional)

Examples:

data = [10, 20, 30, 40, 50]

data[1:4]      # [20, 30, 40]
data[:3]       # [10, 20, 30]
data[2:]       # [30, 40, 50]
data[::2]      # [10, 30, 50]
data[::-1]     # [50, 40, 30, 20, 10]

Slicing does not modify the original sequence; it returns a new one. Negative indices and steps make slicing powerful for reversing or skipping elements.

1 Like

Python slicing is a simple and powerful way to extract parts of a sequence like a string, list, or tuple.

Basic slicing syntax

sequence[start : end : step]

  • start → index to begin from (inclusive)

  • end → index to stop at (exclusive)

  • step → how many steps to move each time

Example with a string

text = "blue_book"
print(text[0:4])   # blue

Starts at index 0 and stops before index 4.

Example with a list

nums = [1, 2, 3, 4, 5, 6]
print(nums[1:5])   # [2, 3, 4, 5]

Using step

print(nums[0:6:2])  # [1, 3, 5]

Takes every second element.

Skipping values

print(text[:4])     # blue (start defaults to 0)
print(text[5:])     # book (end goes till last index)

Negative slicing

print(text[-4:])    # book

Reversing a sequence

print(text[::-1])   # koob_eulb

Key points to remember

  • start is inclusive, end is exclusive

  • Default step is 1

  • Works on strings, lists, tuples, etc.

  • Slicing never throws index errors

Once you get comfortable with slicing, it becomes one of the most useful features in Python for clean and readable code.

1 Like