🐍 Python Beginner

What is slicing in Python?

Why Interviewers Ask This

This is a classic screening question for Python roles. Hiring managers ask it early in interviews to gauge your baseline understanding and determine if you can communicate technical concepts clearly.

Answer

Slicing extracts a portion of sequences (lists, tuples, strings). Syntax: sequence[start:stop:step]. All parameters are optional and default to 0, len, and 1. lst[1:4] — elements at indices 1, 2, 3 (stop is exclusive). lst[:3] — first 3 elements. lst[3:] — from index 3 to end. lst[-3:] — last 3 elements. lst[::2] — every other element. lst[::-1] — reversed list (shallow copy). Slicing always returns a new object (for lists). Assign to a slice: lst[1:3] = [10, 20]. Delete a slice: del lst[1:3]. Strings are also sliceable: "Hello World"[6:]"World". Numpy arrays support multidimensional slicing: arr[0:3, 1:4].

Pro Tip

This topic has Python-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.