Let me preface this by saying I'm new to Python, come from Ruby, and I don't have much specific knowledge about how Python works.
For one of my current projects, I'm creating a new feature in a computational chemistry Django application that reads in PDBs and then does calculations on them. After adding my code, I was getting an error that Python can't typecast a string
as a float
, and looked at the library that parses the PDBs.
I was quickly confused by how Python's slice notation works. For example:
str = 'Hello this is Josh'
str[0:2] #=> 'He'
str[2] #=> 'l'
What I thought calling str[0:2]
would result it would be Hel
, not He
, since index 0
to 2
is 3 big.
Is there a reason that this happens this way, and why str[m:n]
gives from m
to n-1
, not from m
to n
?