Cutting shelf labels: the same arguments, three methods. The board shows where slice, substring and substr really cut. An interactive slice / substring /…
Why it matters
Cutting strings is everywhere: file extensions, text snippets, parsing. Mixing the three up means subtle bugs (swapped arguments, a negative turned into 0). Knowing why slice wins means no more guessing.
The key idea
Three similar methods, but only slice is predictable
Try it in this playground
Move the start below zero slice and substr count from the back; substring takes it as 0 and takes from the front.
Make the start greater than the end slice gives an empty string; substring silently swaps them. Watch its cut points on the board.
Use two negative arguments slice cuts neatly from the back; substring makes both 0 and gives an empty string.
Leave out the second argument All three take to the end, but a negative start is still read differently.