Skip to content

Instantly share code, notes, and snippets.

@chriselgee
Created January 2, 2024 18:12
Show Gist options
  • Save chriselgee/580128a3858b74c8317de62be68ade3e to your computer and use it in GitHub Desktop.
Save chriselgee/580128a3858b74c8317de62be68ade3e to your computer and use it in GitHub Desktop.
Python String Slicing
>>> "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