Pig Latin (10/11)


#1

Hi, so I am working on the Pig Latin translator and I have an issue. I can make the code work perfectly fine and get the new word , but I do not fully understand the part of:

[1:len(new_word)]

Let me explain myself:

  • word[a:b] will give you back the string from index a to index b.
  • len() will give you the number of characters of certain variable.
  • If the index stars in 0, that means that the index of the last letter of a string will be len()-1

Example:

word = notebook

len(word)

will be 8, but if I want to get back the K i would need to code

word[7]

so if I want to get book out of word, I would need to do something like this:

print word[4:7]

or

print word[4:(len(word)-1)]

When in the exercise I write the -1 it deletes the last letter.

I hope it is clear, and that smeone can help me understand. Thanks in advance!


#2

word[7] is accessing string by index, while word[4:7] is string slicing. But with string slicing the slicing goes till values lesser then (<) the the stop value, not lesser then or equal to (<=). So the stop value is not included in the slice


#3

The stop index is excluded from the slice.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.