==运算符不处理两个匹配的字符串

时间:2012-10-03 01:30:37

标签: python string string-matching

我一直在调试此代码,无法让==运算符处理两个匹配的字符串。

代码:

str1 = "string1"
str2 = "string2"

print str1
print str2

for row in results:
  print row[0]
  print row[1]   #as requested
  if((row[0] == str1) and (row[1] == str2)):
     print "We found the match....."
     #rest of the code

1 个答案:

答案 0 :(得分:2)

确保使用.strip()清除空白区域。

>>> str1 = "string1"
>>> str2 = "string2"
>>>
>>> results = [["string1 ", " string2"]]
>>> for row in results:
...   if((row[0].strip() == str1) and (row[1].strip() == str2)):
...      print "We found the match....."
...
We found the match.....
>>>