String characters in Python
We can access any character in a string using its index (sequence number), enclosed in square brackets:
txt = 'abcde'
print(txt[1]) # 'b'
Note that character numbering starts at zero.
You can also find a character from the end of the line. To do this, write its number with a minus sign. In this case, the last character will start with index -1:
txt = 'abcde'
print(txt[-1]) # 'e'
Given a string:
txt = 'abcdef'
Print the first character of this string to the console.
Given a string:
txt = 'abcdef'
Print the second character of this string to the console.
Given a string:
txt = 'abcdef'
Print the last character of this line to the console.
Given a string:
txt = 'abcdef'
Print the second to last character of this line to the console.