Skip to content

Instantly share code, notes, and snippets.

Created December 18, 2012 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4328274 to your computer and use it in GitHub Desktop.
Save anonymous/4328274 to your computer and use it in GitHub Desktop.
Trying to understand string slicing. I don't get why I need to add the very last bit ' + group[-1] ' to get the letter 'y' from 'Mary'
########################################
#
# stringslice-ch7.py
# Algot Runeman
# 2012-12-18
# Trying to understand string slicing
#
########################################
group = "Peter Paul and Mary"
start = 0
# string[x:y] references a 'slice' from the string USE COLON, NOT COMMA!
for ch in range(0,len(group)):
if group[ch] == ' ':
# print ch,'is a space.'
print group[start:ch]
start = ch+1
## Okay, the next slice works but I don't see why I need the last bit.
print group[start:-1]+group[-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment