Sequence Slicing
TypeScript gives you .slice() — Python gives you [start:stop:step]
Section titled “TypeScript gives you .slice() — Python gives you [start:stop:step]”TypeScript arrays have Array.prototype.slice(start, end), which takes two arguments and returns a copy of the subarray. It is useful, but limited: no step parameter, and negative indices only work in a specific way (counting from the end). Strings have String.prototype.slice() with the same interface.
Python’s slice syntax [start:stop:step] is built into the language itself and works on any sequence type — lists, strings, tuples, bytes, and any class that implements __getitem__. The step parameter lets you skip elements or reverse a sequence in one expression. Negative indices count from the end, and any of the three parameters can be omitted.
Basic slicing
Section titled “Basic slicing”// TypeScript: Array.slice(start, end)const data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(data.slice(2, 5)); // [2, 3, 4]console.log(data.slice(0, 3)); // [0, 1, 2]console.log(data.slice(7)); // [7, 8, 9]
// No built-in step — must use filter or manual loopconst evens = data.filter((_, i) => i % 2 === 0);console.log(evens); // [0, 2, 4, 6, 8]
// Reverse — no shorthandconst reversed = [...data].reverse();console.log(reversed);
// Negative index from endconsole.log(data[data.length - 1]); // 9console.log(data.slice(-3)); // [7, 8, 9]# Python: [start:stop:step] — works on any sequencedata = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(data[2:5]) # [2, 3, 4]print(data[:3]) # [0, 1, 2]print(data[7:]) # [7, 8, 9]print(data[::2]) # [0, 2, 4, 6, 8] (every 2nd element)print(data[::-1]) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] (reversed)
# Negative indices count from the endprint(data[-1]) # 9print(data[-3:]) # [7, 8, 9]print(data[1:-1]) # [1, 2, 3, 4, 5, 6, 7, 8]Negative indices
Section titled “Negative indices”TypeScript requires .at() (ES2022) or manual array.length - n math for negative indexing. Python supports negative indices natively on all sequences.
// TypeScript: negative index requires .at() (ES2022) or length mathconst data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(data.at(-1)); // 9 — last elementconsole.log(data.at(-3)); // 7
// Last 3 elements via sliceconsole.log(data.slice(-3)); // [7, 8, 9]
// All but first and lastconsole.log(data.slice(1, -1)); // [1, 2, 3, 4, 5, 6, 7, 8]# Python: negative index works natively everywheredata = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(data[-1]) # 9 — last elementprint(data[-3:]) # [7, 8, 9] — last 3 elementsprint(data[1:-1]) # [1, 2, 3, 4, 5, 6, 7, 8] — all but first and lastSlicing strings and other sequences
Section titled “Slicing strings and other sequences”The same [start:stop:step] syntax works on strings, tuples, and any sequence — not just lists.
// TypeScript: string slicingconst text = "Hello, World!";
console.log(text.slice(7, 12)); // "World"console.log(text.slice(0, 5)); // "Hello"
// Reverse a string — no shorthandconst reversed = text.split("").reverse().join("");console.log(reversed); // "!dlroW ,olleH"# Python: slicing works on strings tootext = "Hello, World!"
print(text[7:12]) # Worldprint(text[:5]) # Helloprint(text[::-1]) # !dlroW ,olleH (reversed in one expression)
# Works on tuples toocoords = (10, 20, 30, 40, 50)print(coords[1:4]) # (20, 30, 40)Try it
Section titled “Try it”data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(data[2:5])print(data[:3])print(data[7:])print(data[::2])print(data[::-1])
print(data[-1])print(data[-3:])print(data[1:-1])
text = "Hello, World!"print(text[7:12])print(text[::-1])Loading Python runtime (first run only)…