Validating where the "@" symbol is in an email. Not validation of the email itself
<p>"it must contain an â@â symbol
which is NEITHER the first character nor the last character in the string."</p>

<p>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.</p>

<pre><code>def validEmail1(myString):
    for i in myString:
        if i == "@":
            return True
    else:
        return False
</code></pre>

<p>With <code>s</code> as your string, you can do:</p>

<pre><code>not (s.endswith('@') or s.startswith('@')) and s.count('@')==1
</code></pre>

<p>Test:</p>

<pre><code>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))
</code></pre>

<p>Prints:</p>

<pre><code>@abc False
abc@ False
abc@def True
abc@@def False
</code></pre>

<hr>

<p>You can also use a slice, but you also need to make sure (indirectly) that the string does not start or end with <code>'@'</code> by making sure the count is 1:</p>

<pre><code>def valid(s):
    return '@' in s[1:-1] and s.count('@')==1
</code></pre>

python python-3.x character email-validation