Python supports positive indexing and negative indexing, allowing you to access characters from either the beginning or the end of a string.
What is String Indexing in Python?
String indexing is the process of accessing a specific character from a string using its index position. In Python, indexing starts from 0, which means the first character has an index of 0, the second character has an index of 1, and so on.Syntax:
Here,string[index]
- string is the string or variable containing the string.
- index is the position of the character you want to access.
# Python program to implement # string indexing language = "Python" print(language[0])Output:
P
Positive Indexing
Positive indexing starts from the left side of the string. The first character has index 0.Consider the following string:
Example:P y t h o n
0 1 2 3 4 5
# Positive indexing in Python language = "Python" print(language[0]) print(language[2]) print(language[5])Output:
P
t
n
Explanation:
- language[0] accesses the first character, P.
- language[2] accesses the third character, t.
- language[5] accesses the sixth character, n.
Negative Indexing
Python also supports negative indexing, which allows you to access characters starting from the right side of a string.
The last character has an index of -1, the second-last character has an index of -2, and the indexing continues in the negative direction.
For the string "Python":
P y t h o n
-6 -5 -4 -3 -2 -1
Example:
# Negative indexing in Python language = "Python" print(language[-1]) print(language[-2]) print(language[-6])Output:
Explanation:n
o
P
- language[-1] accesses the last character, n.
- language[-2] accesses the second-last character, o.
- language[-6] accesses the first character, P.
Positive vs Negative Indexing in Python
| Basis of Comparison | Positive Indexing | Negative Indexing |
|---|---|---|
| Starting Point | Starts from the beginning of the string. | Starts from the end of the string. |
| First Position | The first character has index 0. | The last character has index -1. |
| Direction | Moves from left to right. | Moves from right to left. |
| Example | language[0] returns P. | language[-1] returns n. |
| Best Use | Useful when the position is counted from the beginning. | Useful when accessing characters near the end. |
Accessing Characters Using Variables
You can also use a variable containing an integer as a string index.
Example:
# Python program to access # characters using variables language = "Python" index = 3 print(language[index])
Output:
h
What Happens If You Use an Invalid String Index?
Python raises an IndexError when you try to access a position that does not exist in the string.
Example:
# Python program to show IndexError language = "Python" print(language[10])
Output:
IndexError: string index out of range
The string "Python" contains six characters, so its valid positive indexes range from 0 to 5.
Important Points About String Indexing
1. Indexing Starts from Zero: In positive indexing, the first character of a string always has an index of 0.2. Negative Indexing Starts from -1: The last character of a string always has an index of -1.
3. Spaces Are Also Indexed: Spaces are characters and have their own index positions within a string.
4. Strings Are Immutable: You can access characters using indexing, but you cannot directly change an individual character because Python strings are immutable.text = "Hello World"
print(text[5])
5. String Indexes Must Be Integers: A string index must be an integer value. Using a floating-point number results in a TypeError.language = "Python"
language[0] = "J"
language = "Python"
print(language[1.5])
Common Uses of String Indexing
String indexing is useful in many programming tasks.- Extracting Specific Characters: You can access a particular character when its position is known.
- Validating User Input: Indexing can be used to check specific characters in passwords, codes, or formatted input.
- Processing Text Data: Programs can use indexing to examine and process individual characters in a string.
- Accessing First or Last Characters: Positive and negative indexing make it easy to access the first or last character of a string.
Conclusion
String indexing is a fundamental feature of Python that makes it easy to access individual characters within a string. Python provides both positive and negative indexing, allowing characters to be accessed from either direction. Understanding string indexes is essential for performing operations such as character extraction, validation, searching, and string manipulation.
Frequently Asked Questions (FAQs)
1. What happens when an invalid string index is used?
Python raises an IndexError if the specified index is outside the valid range.
2. Can we modify a string character using indexing?
No. Python strings are immutable, so individual characters cannot be modified directly using indexing.
3. Can string indexing use variables as indexes?
4. Can we use a decimal number as a string index?Yes. A variable containing an integer can be used as an index.
5. Are spaces counted when indexing a string?No. String indexes must be integers. Using a floating-point number as an index raises a TypeError.
Yes. Spaces are characters and have their own index position.
0 Comments