-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?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
vish
  • 235
  • 1
  • 3
  • 16
  • 1
    `if (message == ''): return True` is an option. What do you think? – Austin Feb 10 '20 at 08: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` – DeepSpace Feb 10 '20 at 08:16
  • Duplicate (but I already voted to close as a typo instead): https://stackoverflow.com/questions/9573244/how-to-check-if-the-string-is-empty – Karl Knechtel Oct 07 '22 at 06:06

11 Answers11

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]
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • Why not just use: `return not message or message[0] == message[-1]`? That returns boolean too! – Ajit Panigrahi Feb 10 '20 at 08:23
  • 1
    @AjitZero perfectly correct (and I'll add it), I originally felt it was pushing it a bit too much – DeepSpace Feb 10 '20 at 08:24
  • @DeepSpace kindly look into this Question as well if you can abel to help https://stackoverflow.com/questions/60139211/how-to-set-paths-while-scraping-data-from-website – vish Feb 10 '20 at 08:26
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
SMDC
  • 709
  • 1
  • 9
  • 17
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
  • 3
    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? – Jeremy Caney Jan 16 '22 at 00:30
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

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(""))
Arkenys
  • 343
  • 1
  • 11
  • 2
    Don't hard code `[3]`, as OP wants the first and last characters, use `[-1]` – DeepSpace Feb 10 '20 at 08:14
  • 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. – Arkenys Feb 10 '20 at 08:21
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
Harun Yilmaz
  • 8,281
  • 3
  • 24
  • 35
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(""))
dbc
  • 104,963
  • 20
  • 228
  • 340
O'mondi
  • 19
  • 1
  • 2
    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? – dbc Jan 12 '21 at 16:30
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.

4b0
  • 21,981
  • 30
  • 95
  • 142
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
Omar Munoz
  • 19
  • 6
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(""))
Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
Vidhya
  • 19
  • 1
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)
grgry
  • 1
  • 1