Easy👤 0-2 years👤 3-5 years 1 min read

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

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.

Coffee Chat Question

Concept Made Simple

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

🧠Mind Map Answer

Remember It Faster

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

⌨️Hands-on Keyboard

Learn by Doing

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]

🔥What If?

Think Beyond the Expected

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:stepstop is exclusivenegative indices/step[::-1] reversesslice 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

  • 1Does slicing a list create a copy or a view?
  • 2How would you reverse in place without a copy?
  • 3What does `nums[:]` do and why use it?

🧩Related Technologies

sequencestwo-pointer techniquestr.reverse via reversed()

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?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions