如何在字符串中的字符之前查找单词

时间:2014-03-11 13:36:28

标签: string python-3.x

我正在python中编写一个电子邮件身份验证检查器,我仍然坚持如何在@符号之前检查名称/公司名称。我不知道如何解决这个问题。我检查了@符号和。在域名之前,但是我无法检查这是否是有效的电子邮件,因为它不会在@符号之前检查名称/公司名称。

非常感谢帮助。我也是这个网站的新手,所以这可能是一个非常简单的解决方案。

    def search():
        email = input("Please enter your email adress \n")

    #This checks for the @ symbol in the e-mail adress
        if "@"  in str(email):
            print ("The @ symbol is correct")

        else:
            print("The email adress provided is not vaild because there was no @ symbol")


    #This checks for the . to be present in the email
        if "." in str(email):
            print ("The . is correct")

        else:
            print("The email adress provided is not valid because there was no .")



    #Checks to see if the string only contains letters

        if email.isalpha():
            print('Your email only contains letters')


    #Checks for a symbol clash
        if "@." or ".@" in str(email):
            print("You cannot have two symbols together")





        #This completes the email checking
        if "." in str(email): 
            if "@" in str(email) :
                if ".@" or "@." in str(email)== false:
                    print ("Your email has been registered")

    search()








    #Repeats the process of email checking so that the user can try again
    def main():
        carry_on='y'
        while carry_on=='y':
            search()
            carry_on=input("Try again? (y/n)")

    main()

1 个答案:

答案 0 :(得分:0)

您的问题的答案是使用str.index并切片:

>>> email = "fakeemail@server.com"
>>> email[:email.index('@')]
'fakeemail'

然而,正如凯文在评论中指出的那样,验证电子邮件地址并不像您想象的那么容易。