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.
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.
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]
Step Value (Skipping Elements)
text = "abcdef"
print(text[::2]) # 'ace'
print(text[1::2]) # 'bdf'
Negative Indexing
Python allows negative indices to count from the end.
text = "Python"
print(text[-3:]) # 'hon'
print(text[:-2]) # 'Pyth'
Reversing with Slicing
text = "Python"
print(text[::-1]) # 'nohtyP'
Key Rules to Remember
-
startis included -
endis excluded -
Default
stepis1 -
Slicing never raises an error if indices are out of range
print(text[0:100]) # Works fine
Common Use Cases
-
Extract substrings
-
Reverse sequences
-
Skip elements
-
Copy lists safely
copy_list = nums[:]
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 ![]()
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.
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.