Created
August 25, 2018 12:41
-
-
Save stephaniemdavis/7d9dfedbb3302a1559185cd65587e92d to your computer and use it in GitHub Desktop.
Python string slicing that's independent of string length (counter-intuitively)!!!
This file contains hidden or 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
# 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