FAQ: Introduction to Strings - Negative Indices

This community-built FAQ covers the “Negative Indices” exercise from the lesson “Introduction to Strings”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Negative Indices

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

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’?

The end index is excluded in a slice the same as it is in range(). If you want to go to the end of the string, then leave off the end index.

[-3:]

If you print(second_to_last, final_word) at the end you get “f life”. Hmmmmm :wink:

2 Likes

correct thing to do is:
print(-4:)

The task is:

Use negative indices to find the second to last character in company_motto . Save this to the variable second_to_last .

Here is my code but it says not correct, why ?

company_motto = "Copeland's Corporate Company helps you capably cope with the constant cacophony of daily life"

second_to_last = company_motto[-92:]

I found the solution , the task requires to find the last second to last character so the answer is:


second_to_last = company_motto[-2:]

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

The “penultimate letter” is the “second to last character”.

company_motto[-2:] would select the last two characters i.e. 'fe'

Last two characters ≠ Second to last character
2 characters (plural) ≠ 1 character (singular)

1 Like

Aside

It is well to note that,

a[-2]

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.

1 Like

Very informative remarks regarding slice.

I think instead of None, an empty sequence is returned.

x = [1, 2, 3]
y = x[100:200]
print(y)
[]
print(type(y))
<class 'list'>

s = "abcd"
t = s[4:]
print(t)

print(type(t))
<class 'str'>

u = (1, 2, 3)
v = u[7:]
print(v)
()
print(type(v))
<class 'tuple'>

1 Like

D’oh! And I knew that.

Hello, Im stuck at task 1, what can i start off with?

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.

To preserve code formatting in forum posts, see the thread: [How to] Format code in posts

But, , Value for second_to_last did not match the expected value. :upside_down_face:

bug in excercice checking:

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’

The instructions for Step 1 specify:

Use negative indices to find the second to last character in company_motto. …

It is not asking for the last two characters. Note the use of the word character as opposed to characters in the instructions for Step 1.

The phrase second to last can cause a degree of ambiguity, but the phrase means:

2 Likes

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?

My proposal is to just phrase it 2nd last

Thanks for making this clear!

1 Like