Skip to content

Instantly share code, notes, and snippets.

@stephaniemdavis
Created August 25, 2018 12:41
Show Gist options
  • Save stephaniemdavis/7d9dfedbb3302a1559185cd65587e92d to your computer and use it in GitHub Desktop.
Save stephaniemdavis/7d9dfedbb3302a1559185cd65587e92d to your computer and use it in GitHub Desktop.
Python string slicing that's independent of string length (counter-intuitively)!!!
# Taken from Introduction to Computer Science - Dave Evans of Udacity
s = 'audacity' # Sample string
print(s[:3] + s[3:]
# Counterintuitive Example Below:
t = 'Hi'
print(t[:3] + t[3:] # NO error message despite t's length
# Code below references the character in third index position (which doesn't exist!!)
print(t[3])
# Moral of the story: slicing and indexing-subsetting are different animals
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment