How does slicing work, and how do you reverse a string or list with it?
Reviewed by Gurusankar M.
Short Answer
Slicing is `seq[start:stop:step]` — start inclusive, stop exclusive, step optional. Omitted values default to the ends. A negative step walks backward, so `seq[::-1]` returns a reversed copy — the idiomatic one-liner to reverse a string or list.
On this page
Concept Made Simple
Coffee Chat Question
“How does slicing work, and how do you reverse a string or list with it?”
Remember It Faster
Mind Map Answer
- seq[a:b]
- from a (incl) to b (excl)
- seq[::2]
- every 2nd element
- seq[::-1]
- reversed copy
- seq[:]
- shallow copy of the whole sequence
Learn by Doing
Hands-on Keyboard
s = "python"
print(s[::-1]) # reverse a string
print(s[0:3]) # 'pyt'
nums = [1, 2, 3, 4, 5]
print(nums[-2:]) # [4, 5] last twonohtyp pyt [4, 5]
Think Beyond the Expected
What If?
Is `s[::-1]` the best way to check if a string is a palindrome?
It's the most Pythonic: `s == s[::-1]`. It's O(n) time and simple to read. For huge strings where you want to avoid the reversed copy, a two-pointer loop comparing s[i] and s[-1-i] uses O(1) extra space — but for interviews the slice is the clean, expected answer.
Real World
Slicing shows up everywhere — taking the last N items, dropping a header row, reversing, copying a list before mutating — and `[::-1]` is the reverse trick interviewers expect Python developers to know instantly.
Interviewer's Expectation
Keywords they're listening for:
- start:stop:step
- stop is exclusive
- negative indices/step
- [::-1] reverses
- slice makes a copy
Common Mistakes
- Expecting stop to be inclusive
- Confusing slice copy with a reference
- Off-by-one errors with negative indices
Best Practices
- Use `s[::-1]` to reverse, `s == s[::-1]` for palindrome
- Use `seq[:]` for a quick shallow copy
- Prefer slicing to manual index loops
Follow-up Questions
- Does slicing a list create a copy or a view?
- How would you reverse in place without a copy?
- What does `nums[:]` do and why use it?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Lists (Python) Interview question: "How does slicing work, and how do you reverse a string or list with it?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair