- Syntax
- Example 1: Accessing the First Character
- Example 2: Accessing the Second Character
- Example 3: Accessing the Last Character Using Positive Index
- Example 4: Accessing Characters from a Sentence
- Example 5: Storing an Indexed Character
- Example 6: User Input
- Example 7: Indexing a Numeric String
- Example 8: Using an Index in an Expression
- Example 9: Checking the Type of an Indexed Character
- Example 10: Accessing Multiple Characters Individually
- Common Mistakes
- Best Practices
- Key Points to Remember
A string is a sequence of characters, and each character occupies a specific position called an index.
Python allows you to access individual characters of a string using their index values enclosed within square brackets ([]).
String indexing always starts from 0, which means:
- The first character has an index of
0. - The second character has an index of
1. - The third character has an index of
2, and so on.
Indexing makes it easy to retrieve a single character from a string without affecting the original string.
Syntax #
string_name[index]
Components #
| Component | Description |
|---|---|
string_name |
The string variable. |
[ ] |
Square brackets used for indexing. |
index |
Position of the character (starting from 0). |
Example 1: Accessing the First Character #
main.py
language = "Python"
print(language[0])
Output #
P
Explanation #
"Python"contains six characters.- The first character
Pis at index0. language[0]returns the first character.
Example 2: Accessing the Second Character #
main.py
language = "Python"
print(language[1])
Output #
y
Explanation #
- Index
1refers to the second character. - The character at index
1isy.
Example 3: Accessing the Last Character Using Positive Index #
main.py
language = "Python"
print(language[5])
Output #
n
Explanation #
- The characters of
"Python"are:
| Index | Character |
|---|---|
| 0 | P |
| 1 | y |
| 2 | t |
| 3 | h |
| 4 | o |
| 5 | n |
- The last character has index
5. - Therefore,
language[5]returnsn.
Example 4: Accessing Characters from a Sentence #
main.py
message = "Hello World"
print(message[0])
print(message[6])
Output #
H
W
Explanation #
- Indexing counts every character, including spaces.
His located at index0.- The space occupies index
5. - Therefore,
Wis located at index6.
Example 5: Storing an Indexed Character #
main.py
word = "Computer"
letter = word[3]
print(letter)
Output #
p
Explanation #
- The character at index
3is retrieved. - It is stored in the variable
letter. - Printing
letterdisplaysp.
Example 6: User Input #
main.py
name = input("Enter your name: ")
print(name[0])
Sample Input #
Alice
Output #
A
Explanation #
- The user enters a string.
name[0]retrieves the first character of the entered string.- The first letter of
"Alice"isA.
Example 7: Indexing a Numeric String #
main.py
number = "987654"
print(number[2])
Output #
7
Explanation #
- Although the string contains digits, each digit is treated as a character.
- Index
2refers to the third character. - The returned character is
'7'.
Example 8: Using an Index in an Expression #
main.py
text = "Programming"
print(text[0] + text[1])
Output #
Pr
Explanation #
text[0]returnsP.text[1]returnsr.- The
+operator concatenates the two characters. - The result is
"Pr".
Example 9: Checking the Type of an Indexed Character #
main.py
text = "Python"
character = text[2]
print(character)
print(type(character))
Output #
t
<class 'str'>
Explanation #
- An indexed character is still a string.
- Python does not have a separate
chardata type. - A single character is simply a string of length
1.
Example 10: Accessing Multiple Characters Individually #
main.py
word = "Python"
print(word[0])
print(word[2])
print(word[4])
Output #
P
t
o
Explanation #
- Index
0returnsP. - Index
2returnst. - Index
4returnso. - Each indexed access retrieves one character from the string.
Common Mistakes #
1. Assuming Indexing Starts from 1 #
Incorrect
word = "Python"
print(word[1])
Incorrect Expectation #
P
Actual Output #
y
Reason
Python uses zero-based indexing.
The first character is always at index 0.
Correct
print(word[0])
2. Accessing an Index That Does Not Exist #
Incorrect
word = "Python"
print(word[10])
Output #
IndexError: string index out of range
Reason
The string contains only six characters with indices 0 to 5.
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. Using an Empty String #
Incorrect
text = ""
print(text[0])
Output #
IndexError: string index out of range
Reason
An empty string contains no characters, so there is no valid index.
Best Practices #
- Remember that string indexing starts from
0. - Verify that an index exists before accessing it.
- Use descriptive variable names for strings.
- Remember that every indexed character is itself a string.
- Use indexing when you need to retrieve a single character from a string.
Key Points to Remember #
- Strings are sequences of characters.
- Each character has a unique index.
- Python uses zero-based indexing.
- Square brackets (
[]) are used for indexing. - Index
0always refers to the first character. - Indexing returns a string of length
1. - Accessing an invalid index raises an
IndexError.