Skip to content

9.4 Index Operator: Working with the Characters of a String

  • Indexing operator (Python uses square brackets to enclose the index) selects a single character from a string.

Untitled

  • This can be used to select specific characters in a string
school = "Luther College"
m = school[2]
print(m)

lastchar = school[-1]
print(lastchar)

'''Output:
t
e
'''
  • The expression school[2]selects the character at index 2 from school, and creates a new string containing just this one character. The variable mrefers to the result.
  • Remember that computer scientists often start counting from zero
  • The expression in brackets is called an index