I'm having a bit of trouble with a Python problem that involves strings.
The prompt is:
Modify the
first_and_last
function so that it returnsTrue
if the first letter of the string is the same as the last letter of the string,False
if they’re different. Remember that you can access characters usingmessage[0]
ormessage[-1]
. Be careful how you handle the empty string, which should returnTrue
since nothing is equal to nothing.
This is what I have:
def first_and_last(message):
for char in message:
if char[0] == char[-1]:
return True
elif char == " ":
return True
else:
return False
print(first_and_last("else"))
print(first_and_last("tree"))
print(first_and_last(""))
And the output I'm receiving:
True
True
None
Not quite, first_and_last("tree")
returned True
, should be
False
. Have you added the check for empty strings, and used
correct string indexing? Hint: what do the index numbers of
0 and -1 mean for string handling in Python?
Anyone have any idea how to help?
I'll avoid answering the problem itself and try to give some hints to guide you: