1

I am writing a program which is like an email validator, the user enters their email and then the program checks it has the necessary components. I have to check it contains an @ sign. I want to split the email character by character and put it into a list so that I can loop through the list to get the email.

I currently have this:

email=input('Please enter your email address: ')
mylist=[]
mylist(email)
for i in mylist:
    if i != '@':
        at=True
        print('Your email is invalid because it does not contains an @ sign.')
Sophie Law
  • 37
  • 6

6 Answers6

3

There is no need to convert a string into a list in order to iterate over it. In Python, a string is already iterable, so you can do:

for c in email:
    if c != '@':
        print(...)

But Python offers you a better construct, the in operator:

if '@' not in email:
    print(...)
Right leg
  • 13,581
  • 5
  • 36
  • 68
1

You can just do

if '@' not in email:
    print('Your email is invalid because it does not contains an @ sign.')
puf
  • 111
  • 3
0

If you just need to check if a string has a character inside, you can use:

if char in string:

So in your case:

if '@' in email:
    print("This appears to be a valid email.")
Honza Zíka
  • 467
  • 2
  • 12
0

Why don't you use python-regex? It is relatively fast.

put this in if clause :-

for i in mylist:
email_addr = re.search(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)", i) if email_addr: print email_addr.group() else: print 'incorrect format'

and retreive the address using email_addr.group() and you are good to go.enter code here

0

You can easily use regex to archive what you want

list = ['email@emailprovider.com', 'not_an_email']
for email in list:
    if not re.match('(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)', email):
        print('Your email ({}) is invalid because it does not contains an @ sign.'.format(email))
augustoccesar
  • 598
  • 7
  • 29
0

In Python strings are already iterable. So, you can use the following code to achieve what you want.

email = input("Enter your email")     
if '@' not in email:
    print("The email is invalid as it does not contains @.")
else:
    print("valid email")