EasyExperience level: 0-2 years · 3-5 years1 min read

How does slicing work, and how do you reverse a string or list with it?

Reviewed by Gurusankar M.

Asked inAmazonInfosysAccentureWipro
#slicing#reverse#strings#lists#coding
Report issue

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

python
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 two
Output
nohtyp
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

  1. Does slicing a list create a copy or a view?
  2. How would you reverse in place without a copy?
  3. 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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.