Created
January 2, 2024 18:12
-
-
Save chriselgee/580128a3858b74c8317de62be68ade3e to your computer and use it in GitHub Desktop.
Python String Slicing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> "THIS is what I want"[:4] | |
'THIS' | |
>>> "THIS is what I don't want"[4:] | |
" is what I don't want" | |
>>> "I do not want THIS"[:-4] | |
'I do not want ' | |
>>> "I only want THIS"[-4:] | |
'THIS' | |
>>> "I want THIS only"[7:11] | |
'THIS' | |
>>> "I want THIS only"[-9:-5] | |
'THIS' | |
>>> "I want every other character from 10th on"[10::2] | |
'r te hrce rm1t n' | |
>>> "Reverse SIHT"[11:7:-1] | |
'THIS' | |
>>> "Reverse SIHT"[-1:-5:-1] | |
'THIS' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment