使用python在另一个字符串中查找字符串

时间:2011-09-29 00:54:31

标签: python

假设你有一个电子邮件地址xxx@yyy.com,你想在python中创建这样的东西:

email = 'mystuff@yyy.com'
try:
    if "@yyy.com":
    #do something

    if "@zzz.com":
    #do something else
except:
#do something

我该怎么做?

2 个答案:

答案 0 :(得分:7)

email_addr = "foo@yyy.com"

if email_addr.endswith("@yyy.com"):
    # do something
elif email_addr.endswith("@zzz.com"):
    # do something else

# or...

if "@yyy.com" in email_addr:
    # do something
elif "@zzz.com" in email_addr:
    # do something else

答案 1 :(得分:0)

email = "xxx@yyy.com"
if "@yyy.com" in email:
#do something

if "@zzz.com" in email:
#do something else