anonymous / stringslice-ch7.py
Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

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'

View stringslice-ch7.py
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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.