= "Suzanne"
name = name[3]
fourth_letter print("The fourth character in name is " + fourth_letter)
The fourth character in name is a
Strings are immutable sequences. That means that we can retrieve a character in the string by indexing the string with square brackets. However, we are unable to change characters in the string.
Given the string “Apple”, for example. The first character, “A” is at index 0, the second character “p” is at index 1 and so on:
0 | 1 | 2 | 3 | 4 |
---|---|---|---|---|
A | p | p | l | e |
If we want to retrieve the fourth character in a string, we need to index it at 3:
The fourth character in name is a
We can also use membership tests using the operators in
and not in
with sequences.
For example: