Hi, why is it that when the following is run:
company_motto = “Copeland’s Corporate Company helps you capably cope with the constant cacophony of daily life”
print(company_motto[-1])
print(company_motto[-3:-1])
Prints
e
if
I had expected that print(company_motto[-3:-1]) would have printed ‘ife’?
In my case, I was trying to use second_to_last = company_motto[-2:] but it seems that in codecademy solution it’s second_to_last = company_motto[-2], without : , but like this it’ll only print the penultimate letter and not the second to last character in `company_motto. Maybe the solution from codecademy it’s not the right one
is not a slice. It is an element index that points to the content at that position.
a[-2:]
is a slice. The slice operator, : is what gives it away since without that we cannot perform any slicing operations.
It is important to recognize the difference. By index is singular and direct or absolute. By slice is virtually any length, zero or more, quite indefinite.
Also well to note, slices are virtual so will never raise any errors. An irrelevant reference will simply return None like any undefined return.
company_motto = "Copeland's Corporate Company helps you capably cope with the constant cacophony of daily life"
The task specifies:
Use negative indices to find the second to last character in company_motto. Save this to the variable second_to_last.
Can you share what solution(s) you have tried? That may offer some clues as to the nature of your confusion.
Remark: The task doesn’t want us to print the result. Just wants us to use negative index to target the correct character and then assign it to the specified variable.
Strings - 6/12
it is asking for second to last - it must be [-2:] however it only accepts [-2] as a correct answer which is the second last but not ‘to last’
I see!
Maybe it is better to just rephrase it as I do believe most users at codecademy aren’t native English speakers. Probably even a native speaker might understand it differently, wouldn’t you agree?