Introduction
Indexing and slicing are essential techniques in Python used to access and manipulate sequence data types like strings, lists, and tuples. Indexing allows retrieving specific elements based on their position, while slicing enables extracting a subset of elements from a sequence. These features make data manipulation efficient and flexible.

In this article, you will learn about indexing, slicing, their syntax, and how to use them effectively in Python.
Indexing Strings in Python
Indexing is used to access individual characters in a string. In Python, strings are sequences of characters, and each character has a unique position, known as an index. Indexing in Python starts from 0 for the first character and moves sequentially.
Accessing by Positive Index Number
Python allows you to access characters in a string using positive index numbers. The first character is indexed as 0, the second as 1, and so on.
Example:
text = "Python"
print(text[0])
print(text[1])
print(text[5])
Output:
P
y
n
Explanation:
- text[0] fetches the first character: 'P'.
- text[1] fetches the second character: 'y'.
- text[5] fetches the last character: 'n'.
If you try to access an index that is out of range, Python will raise an IndexError.
Example:
print(text[10]) # IndexError: string index out of rangeAccessing by Negative Index Number
Python also allows negative indexing, which helps when accessing elements from the end of the string. The last character is indexed as -1, the second last as -2, and so on.
Example:
text = "Python"
print(text[-1])
print(text[-2])
print(text[-6])
Output:
n
o
P
Explanation:
- text[-1] fetches the last character: 'n'.
- text[-2] fetches the second last character: 'o'.
- text[-6] fetches the first character: 'P'.
Using negative indexes prevents errors when the length of the string is unknown.



