in Python by
Python supports negative indexes. What are they and why are they used?

1 Answer

0 votes

The sequences in Python are indexed. It consists of positive and negative numbers. Positive numbers use 0 as the first index, 1 as the second index, and so on. Hence, any index for a positive number n is n-1.

Unlike positive numbers, index numbering for the negative numbers start from -1 and it represents the last index in the sequence. Likewise, -2 represents the penultimate index. These are known as negative indexes. Negative indexes are used for:

Removing any new-line spaces from the string, thus allowing the string to except the last character, represented as S[:-1]

Showing the index to representing the string in the correct order