Created
December 18, 2012 14:10
-
-
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'
This file contains 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
######################################## | |
# | |
# 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