Referring to row indices
Each character in a string has a number (called
index), and numbering always starts from zero in many languages.
In Python, you can specify negative indexes. This means it counts from the end of the line.
Example
| String S |
H |
e |
l |
l |
o |
| Index |
S[0] |
S[1] |
S[2] |
S[3] |
S[4] |
| Index |
S[-5] |
S[-4] |
S[-3] |
S[-2] |
S[-1] |
If you add the length of the string to the negative index, you get a "normal" character position.
Need to remember!
In Python, you cannot change a single character in a string, because strings themselves are immutable.