Start learning free

LESSON 64 OF 410 · STRINGS I

Negative Index in Python in Python

Lesson Read the full explanation

Unlike other languages, Python does not throw an error when we try to access string characters with negative indices. They were introduced as an easier way to access characters from the end of a string without using the len() function.

For example, we can use the index -1 to select the last character of a string, even when we don’t know the string's length:

plant = "ficus"

print(plant[-1]) # Output: s

In general, writing string[-x] is equalivalent to writing string[len(string) - x]. We can access every character of a string using negative indices:

car = "ford"

print(plant[-1]) # Output: d
print(plant[-2]) # Output: r
print(plant[-3]) # Output: o
print(plant[-4]) # Output: f

Slicing characters from the end:

Negative indices are also useful for slicing some characters from the end of a string:

message = "Hello world"

# Slicing last 4 characters:
lastChars = message[-4:]
print(lastChars) # Output: orld

# Slicing last 7 characters:
lastChars = message[-7:]
print(lastChars) # Output: o world

In general, if you want to slice the last n characters, you use message[-n:].


Assignment
Follow the Coding Tutorial and let's practice with negative indices!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    PYTHON · STRINGS I

    String Negative Index in Python, explained

    Introduction

    In this lesson, we will explore the concept of negative indexing in Python. Negative indexing is a powerful feature that allows you to access elements from the end of a sequence, such as a string or a list, without needing to know its length. This can simplify your code and make it more readable.

    Negative indexing is particularly useful in scenarios where you need to access the last few elements of a sequence, such as when processing file paths, URLs, or any other data structure where the end elements are of interest.

    Understanding the Basics

    Before diving into negative indexing, it's important to understand how indexing works in Python. In Python, sequences are indexed starting from 0. For example, in the string "hello", the index of 'h' is 0, 'e' is 1, and so on.

    Negative indexing allows you to access elements from the end of the sequence. The index -1 refers to the last element, -2 to the second last, and so on. This is equivalent to using the len() function to calculate the position from the start.

    plant = "ficus"
    
    # Accessing the last character using negative index
    print(plant[-1]) # Output: s
    
    # Equivalent to using len()
    print(plant[len(plant) - 1]) # Output: s
    

    Main Concepts

    Negative indexing can be used not only to access individual elements but also to slice sequences. Slicing with negative indices allows you to extract a sub-sequence from the end.

    message = "Hello world"
    
    # Slicing last 4 characters
    lastChars = message[-4:]
    print(lastChars) # Output: orld
    
    # Slicing last 7 characters
    lastChars = message[-7:]
    print(lastChars) # Output: o world
    

    In general, if you want to slice the last n characters, you use sequence[-n:].

    Examples and Use Cases

    Let's look at some examples to understand how negative indexing can be applied in different contexts:

    # Example 1: Accessing elements in a list
    fruits = ["apple", "banana", "cherry", "date"]
    
    # Accessing the last element
    print(fruits[-1]) # Output: date
    
    # Accessing the second last element
    print(fruits[-2]) # Output: cherry
    
    # Example 2: Slicing a list
    # Slicing the last 2 elements
    print(fruits[-2:]) # Output: ['cherry', 'date']
    
    # Example 3: File path manipulation
    file_path = "/home/user/documents/report.pdf"
    
    # Extracting the file name
    file_name = file_path.split("/")[-1]
    print(file_name) # Output: report.pdf
    

    Common Pitfalls and Best Practices

    While negative indexing is convenient, there are some common mistakes to avoid:

    • Accessing an index that is out of range will raise an IndexError.
    • Be cautious when using negative indices in loops, as it can lead to unexpected behavior if not handled properly.

    Best practices include:

    • Always ensure the index is within the valid range of the sequence.
    • Use negative indexing to simplify your code and make it more readable.

    Advanced Techniques

    Negative indexing can be combined with other advanced techniques such as list comprehensions and lambda functions to create more complex and efficient code.

    # Example: Using negative indexing in a list comprehension
    numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    
    # Extracting the last 3 even numbers
    last_even_numbers = [num for num in numbers[-5:] if num % 2 == 0]
    print(last_even_numbers) # Output: [6, 8, 10]
    

    Code Implementation

    Here is a complete example demonstrating the use of negative indexing in various scenarios:

    # Accessing elements using negative indexing
    plant = "ficus"
    print(plant[-1]) # Output: s
    
    # Slicing using negative indexing
    message = "Hello world"
    print(message[-4:]) # Output: orld
    
    # List example
    fruits = ["apple", "banana", "cherry", "date"]
    print(fruits[-1]) # Output: date
    print(fruits[-2:]) # Output: ['cherry', 'date']
    
    # File path example
    file_path = "/home/user/documents/report.pdf"
    file_name = file_path.split("/")[-1]
    print(file_name) # Output: report.pdf
    
    # Advanced example with list comprehension
    numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    last_even_numbers = [num for num in numbers[-5:] if num % 2 == 0]
    print(last_even_numbers) # Output: [6, 8, 10]
    

    Debugging and Testing

    When working with negative indexing, it's important to test your code thoroughly to ensure it handles edge cases correctly. Here are some tips:

    • Use print statements to verify the values of indices and slices.
    • Write test cases to check the behavior of your code with different input sizes and edge cases.
    import unittest
    
    class TestNegativeIndexing(unittest.TestCase):
        def test_string_indexing(self):
            self.assertEqual("ficus"[-1], 's')
            self.assertEqual("ficus"[-3], 'c')
    
        def test_list_indexing(self):
            fruits = ["apple", "banana", "cherry", "date"]
            self.assertEqual(fruits[-1], "date")
            self.assertEqual(fruits[-2:], ["cherry", "date"])
    
        def test_slicing(self):
            message = "Hello world"
            self.assertEqual(message[-4:], "orld")
            self.assertEqual(message[-7:], "o world")
    
    if __name__ == "__main__":
        unittest.main()
    

    Thinking and Problem-Solving Tips

    When solving problems related to negative indexing, consider the following strategies:

    • Break down the problem into smaller parts and solve each part individually.
    • Use diagrams or write out the sequence to visualize the indices.
    • Practice with different types of sequences to become comfortable with negative indexing.

    Conclusion

    Negative indexing is a powerful feature in Python that allows you to access and manipulate elements from the end of a sequence easily. By understanding and mastering this concept, you can write more efficient and readable code. Practice with different examples and scenarios to become proficient in using negative indexing.

    Additional Resources

    For further reading and practice, consider the following resources:

    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free