String 如何在字符串中查找字符前的单词
我正在用python编写一个电子邮件身份验证检查器,我一直在研究如何在@符号之前检查名称/公司名称。我不知道如何避开这个问题。我已经检查了@符号和。在域之前,但我无法检查这是否是有效的电子邮件,因为它不会在@符号之前检查名称/公司名称 非常感谢您的帮助。我也是这个网站的新手,所以这可能是一个非常简单的解决方案String 如何在字符串中查找字符前的单词,string,python-3.x,String,Python 3.x,我正在用python编写一个电子邮件身份验证检查器,我一直在研究如何在@符号之前检查名称/公司名称。我不知道如何避开这个问题。我已经检查了@符号和。在域之前,但我无法检查这是否是有效的电子邮件,因为它不会在@符号之前检查名称/公司名称 非常感谢您的帮助。我也是这个网站的新手,所以这可能是一个非常简单的解决方案 def search(): email = input("Please enter your email adress \n") #This checks
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()
您的问题的答案是使用
str.index
和切片:
>>> email = "fakeemail@server.com"
>>> email[:email.index('@')]
'fakeemail'
然而,正如Kevin在评论中指出的,验证电子邮件地址并不像你想象的那么容易。你可以在电子邮件中包含“@”和“@.”。例如,“very.unspecial.@.unspecial.com”@example.com
是一个有效的电子邮件地址。确定电子邮件地址是否有效的最简单方法是,向该地址发送电子邮件并等待回复。