Wiggs Wiggs - 1 year ago 67
Python Question

Validating where the "@" symbol is in an email. Not validation of the email itself

"it must contain an “@” symbol
which is NEITHER the first character nor the last character in the string."

So I have this assignment in my class and I can't for the life of me figure out how to make the boolean false for having it at the front or end of the email. This is what I have so far.

def validEmail1(myString):
for i in myString:
if i == "@":
return True
return False

Answer Source

With s as your string, you can do:

not (s.endswith('@') or s.startswith('@')) and s.count('@')==1


def valid(s):
    return not (s.endswith('@') or s.startswith('@')) and s.count('@')==1

cases=('@abc','abc@','abc@def', 'abc@@def')

for case in cases:
    print(case, valid(case))


@abc False
abc@ False
abc@def True
abc@@def False