-1

I had coded a simple python program for substring comparison but it is giving me wrong answer , it should print the match is found after matching

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");
fredtantini
  • 15,966
  • 8
  • 49
  • 55
  • possible duplicate of [Why does comparing strings in Python using either '==' or 'is' sometimes produce a different result?](http://stackoverflow.com/questions/1504717/why-does-comparing-strings-in-python-using-either-or-is-sometimes-produce) – fredtantini Jan 09 '15 at 11:42
  • Other good answers [here](http://stackoverflow.com/questions/2988017/string-comparison-in-python-is-vs). – fredtantini Jan 09 '15 at 11:42

1 Answers1

0

Use == to compare strings:

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

is compares object identity. In this case you have two different string objects with same contents.

Janne Karila
  • 24,266
  • 6
  • 53
  • 94