{"id":"strings-slices","slug":"slices","title":"Slices","text":"An index gives you one character. A **slice** gives you a piece of a string, a **substring**: `text[start:end]` takes the characters from the index `start` up to, but not including, the index `end`.\n\n```python\ntext = \"basketball\"\nprint(text[0:6])\nprint(text[6:10])\nprint(text[2:5])\n```\n\n```output\nbasket\nball\nske\n```\n\nIt helps to picture the indexes as marks between the characters, with the mark 0 before the first character. A slice takes what lies between two marks:\n\n```text\n| b | a | s | k | e | t | b | a | l | l |\n0   1   2   3   4   5   6   7   8   9   10\n```\n\nSo `text[0:6]` is everything between the marks 0 and 6. Because the end is left out, the length of a slice is simply `end - start`: `text[2:5]` has 3 characters.\n\n### Leaving out the start or the end\n\nLeave out the start, and the slice begins at the start of the string. Leave out the end, and it goes on to the end. Negative indexes work in slices too:\n\n```python\ndate = \"2026-10-15\"\nprint(date[:4])\nprint(date[5:7])\nprint(date[-2:])\n```\n\n```output\n2026\n10\n15\n```\n\n`date[:4]` is the first four characters, and `date[-2:]` the last two.\n\n### Slices don't run out\n\nUnlike an index, a slice that goes past the end of the string doesn't stop the program. It takes whatever characters there are, perhaps none at all:\n\n```python\nword = \"cat\"\nprint(word[1:100])\nprint(\"[\" + word[5:9] + \"]\")\n```\n\n```output\nat\n[]\n```\n\nA loop can move a slice along a string. This one prints a code in pieces of three characters; the last piece is shorter, since the slice stops at the end of the string:\n\n```python\ncode = \"ABCDEFGH\"\nstart = 0\nwhile start < len(code):\n    print(code[start:start + 3])\n    start += 3\n```\n\n```output\nABC\nDEF\nGH\n```\n","task":"Fill in the slices so the program prints the hours, the minutes and the seconds of the time.\n\n```\nHours: 14\nMinutes: 35\nSeconds: 20\n```\n","kind":"fill","seed":"time = \"14:35:20\"\nprint(\"Hours: \" + time[[[0:2‖:2]]])\nprint(\"Minutes: \" + time[[[3:5‖-5:-3]]])\nprint(\"Seconds: \" + time[[[6:8‖6:‖-2:]]])\n","solution":"time = \"14:35:20\"\nprint(\"Hours: \" + time[0:2])\nprint(\"Minutes: \" + time[3:5])\nprint(\"Seconds: \" + time[6:8])\n","hints":["Write the indexes under the characters: `1` is at 0, `4` at 1, the first `:` at 2, and so on.","A slice `start:end` stops just before `end`. The hours are the characters at 0 and 1, so their slice is `0:2`."],"tests":[{"name":"Output","stdin":"","expect":"Hours: 14\nMinutes: 35\nSeconds: 20","hidden":false}],"require":[],"forbid":[],"more":[{"task":"Write a program that asks for a card number and prints it with every character except the last four replaced by `*`. The number always has at least four characters.\n\n```\nCard number: 1234567812345678\n************5678\n```\n","kind":"code","seed":"number = input(\"Card number: \")\n","solution":"number = input(\"Card number: \")\nhidden = \"*\" * (len(number) - 4)\nprint(hidden + number[-4:])\n","hints":["The last four characters are a slice from the end: `number[-4:]`.","The stars replace all the other characters, so there are `len(number) - 4` of them. Repeat `\"*\"` that many times."],"tests":[{"name":"Test 1","stdin":"1234567812345678\n","expect":"Card number: 1234567812345678\n************5678","hidden":false},{"name":"Test 2","stdin":"98765\n","expect":"Card number: 98765\n*8765","hidden":true},{"name":"Test 3","stdin":"4321\n","expect":"Card number: 4321\n4321","hidden":true},{"name":"Test 4","stdin":"5555666677778888999\n","expect":"Card number: 5555666677778888999\n***************8999","hidden":true}],"require":[],"forbid":[]},{"task":"Write a program that asks for a word and prints it smaller and smaller: each line drops one character from both ends and is moved one space to the right, as long as there are characters left.\n\n```\nWord: abcdefg\nabcdefg\n bcdef\n  cde\n   d\n```\n","kind":"code","seed":"word = input(\"Word: \")\n","solution":"word = input(\"Word: \")\ncut = 0\nwhile cut < len(word) - cut:\n    print(\" \" * cut + word[cut:len(word) - cut])\n    cut += 1\n","hints":["Use a helper variable `cut` for how many characters are dropped from each end. It starts at 0 and grows by 1 in every round.","The piece of the word is the slice from `cut` up to `len(word) - cut`. Put `cut` spaces in front of it.","The loop goes on while the slice still has characters, that is while `cut < len(word) - cut`."],"tests":[{"name":"Test 1","stdin":"abcdefg\n","expect":"Word: abcdefg\nabcdefg\n bcdef\n  cde\n   d","hidden":false},{"name":"Test 2","stdin":"abcdef\n","expect":"Word: abcdef\nabcdef\n bcde\n  cd","hidden":true},{"name":"Test 3","stdin":"x\n","expect":"Word: x\nx","hidden":true},{"name":"Test 4","stdin":"palindrome\n","expect":"Word: palindrome\npalindrome\n alindrom\n  lindro\n   indr\n    nd","hidden":true}],"require":[],"forbid":[]}]}