Explore slicing with start, stop, and step.

QuestionsQuestions8 SkillsProDec, 21 2025
0179

Let's dive into the start, stop, and step formula of slicing. This is one of the most powerful features in Python for manipulating data.

The full syntax is: object[start : stop : step]

1. start and stop

  • start: The index where the slice begins (included).
  • stop: The index where the slice ends (not included).

Try this in your terminal with your full_name ("Natchaya Tabnonthong"):

# Get the first 4 letters
print(full_name[0:4])  # Output: 'Natc'

# If you leave 'start' empty, it defaults to the beginning
print(full_name[:8])   # Output: 'Natchaya'

# If you leave 'stop' empty, it goes to the very end
print(full_name[9:])   # Output: 'Tabnonthong'

2. The step (The hidden power)

The step determines the increment between each character. By default, it is 1.

# Create a simple string to see it clearly
numbers = "0123456789"

# Every second character (even numbers)
print(numbers[::2])    # Output: '02468'

# Start at 1, every second character (odd numbers)
print(numbers[1::2])   # Output: '13579'

3. The "Reverse" Trick

If you use a negative step, Python slices backward. This is the easiest way to reverse a string in Python:

# Reverse your whole name!
print(full_name[::-1])

Summary of Shortcuts:

  • [:] -> Copy of the whole string.
  • [start:] -> Everything from start to the end.
  • [:stop] -> Everything from the beginning up to stop.
  • [::-1] -> The whole string reversed.

Try it yourself: Can you write a slice that prints every 3rd letter of your name? It would look like full_name[::3]. Give it a shot!

0 Comments

no data
Be the first to share your comment!