educative.io

Shouldn't the ! not be there

my_string = “This is MY string!”
print(my_string[8:len(my_string)])

given output: MY string!

It is mentioned that The character at the end index in the string, will not be included in the substring obtained through this method.
Since the end of the substring is len(my_string) the last character of the string is ! and thus that should not be included. Instead shouldn’t the output be just

MY string

Hi @Aksshita_S

Sorry for making it a bit confusing, let me clarify it
when we try to slice the string, we do the following thing
string_name[start:end]

For example:

my_string = “This is MY string!”
print(len(my_string[1:4]))

The output will be 3, not 4, because 4 is not included. Similarly in print(my_string[8:len(my_string)])
the length of my_string is 18, and since it starts from the 0th index, that means there isn’t anything on the 18th index, and the last index is 17th, which holds “!” as shown in the diagram below

I hope this response was helpful but if I have left something out. please let me know.

1 Like

oh so this must be the reason why we start from 0 while indexing!
thankyou for your answer. it cleared the same doubt which i had!!