public
anonymous / stringslice-ch7.py
Created

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'

  • Download Gist
stringslice-ch7.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
########################################
#
# 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]

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.