关于子串的匹配

时间:2015-01-09 11:37:09

标签: python-2.7

我编写了一个简单的python程序进行子字符串比较,但它给了我错误的答案 ,匹配后应打印the match is found

def myfirst_yoursecond(p,q):

    a = p.find(" ")
    c = p[:a]
    print "the first word is:",c

##Storing the other "bell" word of "p" string in c

    a1 = q.find(" ")
    c1 = q[a1+1:]
    print "The second word is:",c1

## Storing the other "bell" word of "q" string in c1


## comparing the string
    if ( c is c1 ) :
        print "the match is found"
    else:
        print "not found"


## function call
myfirst_yoursecond("bell hooks","curer bell");

1 个答案:

答案 0 :(得分:0)

使用==比较字符串:

if c == c1:
    print "the match is found"
else:
    print "not found"

is比较对象标识。在这种情况下,您有两个具有相同内容的不同字符串对象。