当前位置: 动力学知识库 > 问答 > 编程问答 >

python 2.7 - reversing and comparing original and altered strings

问题描述:

I declared two variables as follows (one of which is the reverse of the other):

a = "test"

b = "tset" # reverse of a

I then ran this into the interpretor which returned the below value:

>>> b[::-1]

'test' # value returned

As you can see, it reversed the string which made it exactly like the first variable a.

However, when I execute this statement, the results are not quite the same:

a is b[::-1]

False # returns false after executing above statement

a is "test", so is b[::-1]. So why is it that the condition does not evaluate to True?

网友答案:

To check for "values" equality you should use the operator ==:

>>> a == b[::-1]
True

So why is it that the condition does not evaluate to True?

The is keyword is used to compare objects identity; in other words, if the objects are the same.

分享给朋友:
您可能感兴趣的文章:
随机阅读: