Introduction
String slicing helps you get parts of a word or sentence easily, like cutting a piece from a cake.
Jump into concepts and practice - no test required
string[start:stop:step]
text = "hello" print(text[1:4])
text = "world" print(text[:3])
text = "python" print(text[2:])
text = "abcdef" print(text[::2])
word = "programming" # Get first 4 letters first_part = word[:4] # Get letters from position 3 to 7 middle_part = word[3:8] # Get last 3 letters last_part = word[-3:] # Reverse the word reversed_word = word[::-1] print(f"First 4 letters: {first_part}") print(f"Letters 3 to 7: {middle_part}") print(f"Last 3 letters: {last_part}") print(f"Reversed word: {reversed_word}")
s[2:5] do on the string s = 'Python'?s starting from the first character?s[::2] means start at 0, go to end, step by 2, so every second character.s = 'abcdefg' print(s[5:2:-1])
s = 'hello' print(s[4:1])
s = 'abcdefgh', which slice extracts the string 'hgfed'?