• Home
  • 4.3 Negative Indexing

4.3 Negative Indexing

View Categories

4.3 Negative Indexing

2 min read

Python allows you to access characters from the end of a string using negative indexing.

Instead of counting from the beginning, negative indexing starts counting from the end of the string.

The last character has an index of -1, the second last character has an index of -2, the third last character has an index of -3, and so on.

Negative indexing is useful when you need to access characters near the end of a string without knowing its exact length.


Syntax #

string_name[negative_index]

Components #

Component Description
string_name The string variable.
[ ] Square brackets used for indexing.
negative_index Position counted from the end of the string.

Example 1: Accessing the Last Character #

main.py

language = "Python"

print(language[-1])

Output #

n

Explanation #

  • Negative indexing starts from the end.
  • Index -1 always refers to the last character.
  • Therefore, language[-1] returns n.

Example 2: Accessing the Second Last Character #

main.py

language = "Python"

print(language[-2])

Output #

o

Explanation #

  • Index -2 refers to the second last character.
  • The second last character in "Python" is o.

Example 3: Accessing Multiple Characters #

main.py

language = "Python"

print(language[-1])
print(language[-3])
print(language[-6])

Output #

n
h
P

Explanation #

  • -1 returns the last character n.
  • -3 returns the third character from the end, which is h.
  • -6 refers to the first character because the string has six characters.

Example 4: Understanding Negative Indices #

main.py

word = "Python"

print(word[-4])

Output #

t

Explanation #

The characters in "Python" are indexed as follows:

Positive Index Character Negative Index
0 P -6
1 y -5
2 t -4
3 h -3
4 o -2
5 n -1
  • Therefore, word[-4] returns t.

Example 5: User Input #

main.py

name = input("Enter your name: ")

print(name[-1])

Sample Input #

Alice

Output #

e

Explanation #

  • The user enters a string.
  • name[-1] retrieves the last character.
  • The last character of "Alice" is e.

Example 6: Negative Indexing on a Numeric String #

main.py

number = "123456"

print(number[-2])

Output #

5

Explanation #

  • Each digit is treated as a character.
  • -2 refers to the second last character.
  • The returned character is '5'.

Example 7: Combining Positive and Negative Indexing #

main.py

text = "Python"

print(text[0])
print(text[-1])

Output #

P
n

Explanation #

  • Positive indexing starts from the beginning.
  • Negative indexing starts from the end.
  • Both methods can be used in the same program.

Example 8: Storing the Last Character #

main.py

word = "Computer"

last = word[-1]

print(last)

Output #

r

Explanation #

  • word[-1] retrieves the last character.
  • It is stored in the variable last.
  • Printing last displays r.

Example 9: Checking the Type #

main.py

text = "Python"

character = text[-3]

print(character)
print(type(character))

Output #

h
<class 'str'>

Explanation #

  • Negative indexing returns a single character.
  • A single character is still of type str.
  • Python does not have a separate character data type.

Example 10: Accessing Every Character from the End #

main.py

word = "CAT"

print(word[-1])
print(word[-2])
print(word[-3])

Output #

T
A
C

Explanation #

  • -1 returns the last character.
  • -2 returns the second last character.
  • -3 returns the first character.
  • This demonstrates how negative indexing moves backward through the string.

Common Mistakes #

1. Assuming -1 Refers to the First Character #

Incorrect

word = "Python"

print(word[-1])

Incorrect Expectation #

P

Actual Output #

n

Reason

Negative indexing starts from the end of the string.
-1 always refers to the last character.


2. Accessing an Invalid Negative Index #

Incorrect

word = "Python"

print(word[-7])

Output #

IndexError: string index out of range

Reason

The string contains only six characters.
The valid negative indices are -1 through -6.


3. Using a Floating-Point Index #

Incorrect

word = "Python"

print(word[-2.0])

Output #

TypeError: string indices must be integers

Reason

String indices must always be integers.


4. Accessing an Empty String #

Incorrect

text = ""

print(text[-1])

Output #

IndexError: string index out of range

Reason

An empty string contains no characters.
Therefore, no valid negative index exists.


Best Practices #

  • Use negative indexing when accessing characters near the end of a string.
  • Remember that -1 always refers to the last character.
  • Verify that the string is not empty before accessing negative indices.
  • Use positive indexing when accessing characters near the beginning of a string.
  • Choose the indexing method that makes your code easier to read.

Key Points to Remember #

  • Negative indexing accesses characters from the end of a string.
  • The last character has index -1.
  • The second last character has index -2.
  • Positive and negative indexing can be used together.
  • Indexed characters are strings of length 1.
  • Accessing an invalid negative index raises an IndexError.
  • String indices must always be integers.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *