Chapter 12: String Indexing

We can extract a single character from a string using indexing. The index starts from 0 for the first character and goes up to n-1 for the last character.

Additionally, we can access characters starting from the end of the string without knowing its length. In this case, the indexing starts from -1 for the last character, -2 for the second last, and so on. An example is provided below.

 

  F r e e   L e a r n i n g
Indexing from first word 0 1 2 3 4 5 6 7 8 9 10 11 12
Indexing from the last word -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1

 

name = "Free Learning"

print(name[-1]) # printing from last word

Output: g

print(name[1]) # printing from first word

Output: r