Help! i dont get it


#1

pyg = 'ay'
original = raw_input('Enter a word:')
if len(original) > 0 and original.isalpha():
print original
word = original.lower()
first = word[0]
new_word = word + first + pyg
else:
print 'empty'
print new_word[1:len(new_word)]

on the last line, it isnt suposed to take from the index 1 to the len(new_word)-1? i tried the code with "len(new_word)" and "len(new_word)+1" to "fix" what i though was necesary, but in both ways i got the same result: its ok!!!. someone could explain it please? thanks!!!


#2

Hi just move that line print new_word[1:len(new_word)] inside the if statement amd set it equale to a new new_word like that new_word = new_word[1:len(new_word)] then add a print statment that will print the new word just like that print new_word


#3

hi @wizmarco!!! thanks for your quick answer. maybe i didnt explain my question right, but my problem is that i dont understand why the code do consider the last index of the [1:len(...)], because, as i understood, the code should not consider the last parameter of the range, i am right? or is that in the case that the range does consider the last index it is consider by the code too?


#4

Hi,
the len() gives out the lenght of for example the word "hello" with 5, in case of the indexing alltime starts at 0, so you are right that index[5] in this example will not be printed. But if you would use the above calling of print sth[1:len(sth)-1) it would cut, as wanted, the first letter at index [0] but as even, as not wanted, the last letter on index[4], too, because the highest digit, in this example with the word "hello" 5, will not be printed.

If a word = "hello" and you will print it out, the
first index is [0:h]
the second [1:e]
the third [2:l]
the fourth [3:l] and
the fifth [4:o]

But in case of len(word) you will get the digit 5, so the calling of

print word[0:len(word)] is the same as
print word[0:5]
what means 6 indexes of which the last one will not be called, so your print out will be
hello
word[0:5] > 6 indexes > first index[0] > last index[5] not progressed

I hope I could explain it in an understandable way :slightly_smiling:
And if not, please feel free to ask

Greets