Extract Substrings with Slicing Tricks

Easy ⏱ 8 min 85% acceptance ★★★★☆ 4.3
Write slice_demo(s) that returns a dictionary with five keys computed purely via slicing: "first3" (first 3 characters), "last3" (last 3 characters), "middle" (everything except the first and last 2 characters), "reversed" (the whole string reversed), and "every_other" (every second character, starting from index 0).

Examples

Example 1
Input
s = 'DataVix2026'
Output
{'first3': 'Dat', 'last3': '026', 'middle': 'taVix20', 'reversed': '6202xiVataD', 'every_other': 'DtVx06'}
Explanation

Each key is produced by a different slice expression on the same 11-character string.

Constraints

  • len(s) >= 5, so first3/last3/middle are always well defined.

Topics

StringsSlicing

Companies

Goldman SachsAtlassian

Hints

Hint 1

s[:3], s[-3:], s[2:-2], s[::-1], s[::2] cover all five.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.