-2

In Python, if i wanted to write something that returns a boolean True as long as the @ symbol appears after the . symbol, how would I write this?

I have tried writing something like:

if myString[x] == "@" > myString[x] == ".":
     return True

but, I assume that this won't work because they're both essentially the same index. Looking for some explanation as well, just learning a lot of basics still. Thanks!

Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
  • 1
    you want to know if the @ appears after or before `.` for email validation? – depperm Sep 30 '16 at 19:56
  • 3
    `this.is.a.valid@email.address` and you algorithm fails this. – kennytm Sep 30 '16 at 19:58
  • For email address checking, see http://stackoverflow.com/questions/8022530/python-check-for-valid-email-address. – kennytm Sep 30 '16 at 20:00
  • I appreciate the info, I'm actually just trying to write this poor function for an assignment and having trouble. I understand that this wouldn't work well if I tried to implement this in the real world. Thanks for the info, though! –  Sep 30 '16 at 20:17
  • If it is homework, you should do it yourself. If you don't know the basics, like how to find the position of `@` and `.` in a string, google *"find position of character in string python"* – zvone Sep 30 '16 at 21:56
  • Okay, will do. Thanks –  Sep 30 '16 at 22:00

3 Answers3

1
if str.index('@') > str.index('.'):
   return True;
FallAndLearn
  • 4,035
  • 1
  • 18
  • 24
1

You could use a simple regular expression:

import re

rx = re.compile(r'\S+@\S+')

string = """This text contains an email@address.com somewhere"""

if rx.search(string):
    print("Somewhere in there is an email address")

See a demo on ideone.com.

Jan
  • 42,290
  • 8
  • 54
  • 79
1

return str.find('@') > str.find('.') and str.find('.') > -1

This will check that @ is after ., and that both symbols are present in the string. This is however a very bad way to check for email validity.

Efferalgan
  • 1,681
  • 1
  • 14
  • 24