When trying to print the middle character of a string using the following code:
def mid(a):
return a[(len(a) + 1) / 2]
print(mid("abc"))
I get the error:
TypeError: string indices must be integers
Changing it to:
def mid(a):
return a[(len(a) - 1) // 2 : (len(a) + 2) // 2]
print(mid("abc"))
outputs:
b
Why does directly grabbing the middle value return the TypeError, but grabbing the 0th value and then the middle value actually work?