// 两个相同的单字符 >>> a = 'a' >>> b = 'a' >>> id(a), id(b) (140478486488208, 140478486488208) >>> a is b True // 两个相同的空字符串 >>> a = '' >>> b = '' >>> id(a), id(b) (140377617439408, 140377617439408) >>> a is b True // 两个相同空格的字符串 >>> a = ' ' >>> b = ' ' >>> id(a), id(b) (140377616103608, 140377616103608) >>> >>> a is b True # 两个相同值的字符串,是同一个对象,共享引用 >>> a = 'liunx' >>> b = 'liunx' >>> id(a), id(b) (140478486363976, 140478486363976) >>> a is b True # 两个相同值但长度不超过20位的字符串,是同一个对象,共享引用 >>> a = 'linux' * 4 >>> b = 'linux' * 4 >>> id(a), id(b) (140478486391736, 140478486391736) >>> a is b True # 两个相同值但长度超过20位的字符串,不是同一个对象 >>> a = 'linux' * 5 >>> b = 'linux' * 5 >>> id(a), id(b) (140478486354640, 140478486355280) >>> a is b False # 两个相同值但包含特殊字符串(非大小写字母和数字或下划线)的,不是同一个对象 >>> a = '**' >>> b = '**' >>> a is b False >>> a = '_-' >>> b = '_-' >>> a is b False >>> a = 'a*c' >>> b = 'a*c' >>> a is b False >>> a = 'lin ux' >>> b = 'lin ux' >>> id(a), id(b) (140478486364088, 140478485729384) >>> a is b False
以上是在交互模式下的结果,但是在pycharm中运行如下代码得到的结果会是True
1 2 3 4 5 6
c = "hello world" d = "hello world" print(c == d) True print(c is d) True