-1
def first_and_last(message):
    if (message[0] == message[3]):
        return True
    elif (message[0] != message[3]):
        return False

print(first_and_last("else"))
print(first_and_last("tree"))
print(first_and_last(""))

I want to return True if string is empty, True if 1st and last letter of string match and False otherwise.

How can I get a True result for an empty string?

3
  • 1
    if (message == ''): return True is an option. What do you think? Commented Feb 10, 2020 at 8:10
  • Do you want to compare with the last character or the fourth? if it's the former, you should use [-1] instead of [3]. If you use [3] then first_and_last("abcad") will return True Commented Feb 10, 2020 at 8:16
  • Duplicate (but I already voted to close as a typo instead): stackoverflow.com/questions/9573244/… Commented Oct 7, 2022 at 6:06

11 Answers 11

4
  • You wrote that you want to compare the first character to the last, so you have to use [-1] and not [3]. Otherwise you are comparing the first and the fourth characters.

  • You can use if not message to check if it's an empty string

  • Since you are returning, you don't need to check if they do not match.


def first_and_last(message):
    if not message:
        return True
    return message[0] == message[-1]

As noted in the comments this can be pushed a bit more into a single line:

def first_and_last(message):
    return not message or message[0] == message[-1]
Sign up to request clarification or add additional context in comments.

3 Comments

Why not just use: return not message or message[0] == message[-1]? That returns boolean too!
@AjitZero perfectly correct (and I'll add it), I originally felt it was pushing it a bit too much
@DeepSpace kindly look into this Question as well if you can abel to help stackoverflow.com/questions/60139211/…
2

You can use if not

def first_and_last(message):
if not message:
    return False
else: return True


print(first_and_last("else")) #True
print(first_and_last("tree")) #True
print(first_and_last("")) #False

Comments

2
# defines, name and establishes the arguments for the function
def first_and_last(message):
 
    # "if not message" checks if the parameter is empty
    # message[0] gets the first character in the parameter
    # message[-1] gets the last character in the parameter
    # "==" checks if the first and last character are the same in the 
    # parameter
    # the "or" comparator checks if the statements on either side or 
    # the comparator is true 

    if not message or message[0] == message[-1]:
        # if either one or both of the above statements are true the 
        # function returns True

        return True
    # otherwise returns False

    return False

1 Comment

Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?
1

You can use if not message: to check if the message variable is empty.

You could, for example, do as follow :

def first_and_last(message):
    if not message:
        return True
    elif (message[0] == message[3]):
            return True
    elif (message[0] != message[3]):
            return False


print(first_and_last("else"))
print(first_and_last("tree"))
print(first_and_last(""))

2 Comments

Don't hard code [3], as OP wants the first and last characters, use [-1]
@DeepSpace I don't hard code [3]. I took back the code the author provided to solution the problem. I don't think it's necessary to comment all the posts in the answers to notice the same thing. howerver, you can fully notify the author of the post that the last character in a python list/str... is returned by the [-1] call.
1

You can check if the string is empty with bool(message). And check the last item with message[-1]:

def first_and_last(message):
    if not message:
        return True
    elif (message[0] == message[-1]):
        return True
    else:
        return False


print(first_and_last("else")) # Returns True
print(first_and_last("tree")) # Returns False
print(first_and_last("")) # Returns True

2 Comments

if bool(...) is redundant. Just if not message is sufficient
Also, don't hard code [3], as OP wants the first and last characters, use [-1]
1

Use:

if not message:

This will return true if the string is empty:

def first_and_last(message):
        if not message:
            return True
        if (message[0] == message[3]):
            return True
        elif (message[0] != message[3]):
            return False


print(first_and_last("else"))
print(first_and_last("tree"))
print(first_and_last(""))

1 Comment

Don't hard code [3], as OP wants the first and last characters, use [-1]
1
def first_and_last(message):
    if not message:
        return True
    elif (message[0] == message[-1]):
        return True
    elif (message[0] != message[-1]):
        return False


print(first_and_last("else"))
print(first_and_last("tree"))
print(first_and_last(""))

1 Comment

Hello, and welcome to stack overflow. Might you please edit your answer to explain a little bit how this answer improves on the other answers?
1
def first_and_last(message):

if message=="" or message[0]==message[-1]:

    return True

return False

Here, message == "" checks for empty string and after "or" it checks for first and last letter of word.

Comments

1

Just to make it more readable since you are learning and haven't used "if not"

def first_and_last(message):
    if message == "":
        return True
    elif message[0] == message[-1]:
        return True  
    else:
        return False

print(first_and_last("else"))
print(first_and_last("tree"))
print(first_and_last(""))

output would be  below 

True
False
True

Comments

1
def first_and_last(message):
    if message == "" or message[0] == message[-1]:
        return True
    else:
        return False

print(first_and_last("else"))
print(first_and_last("tree"))`enter code here`
print(first_and_last(""))

Comments

0
# could it work like this as well? if not why? thanks :)
def first_and_last(message):
while bool(message) == True:
    first_letter = str(message[0])
    last_letter = str(message[-1])
    if first_letter == last_letter:
        return True
    else :
        return False
 return True

 print(first_and_last("else"))
 print(first_and_last("tree"))
 print(first_and_last(""))
 # Output (True, False, True)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.