python / beginner
Snippet
String Slicing
Slicing allows you to extract a specific portion of a string by defining a start and end index.
snippet.py
1
2
3
word = "Python"part = word[0:2]print(part)
Breakdown
1
word = "Python"
Creates a string variable named 'word'.
2
part = word[0:2]
Extracts characters from index 0 up to (but not including) index 2.
3
print(part)
Displays the result, which in this case is 'Py'.