我的编程空间,编程开发者的网络收藏夹
学习永远不晚

Python string compar

短信预约 -IT技能 免费直播动态提醒
省份

北京

  • 北京
  • 上海
  • 天津
  • 重庆
  • 河北
  • 山东
  • 辽宁
  • 黑龙江
  • 吉林
  • 甘肃
  • 青海
  • 河南
  • 江苏
  • 湖北
  • 湖南
  • 江西
  • 浙江
  • 广东
  • 云南
  • 福建
  • 海南
  • 山西
  • 四川
  • 陕西
  • 贵州
  • 安徽
  • 广西
  • 内蒙
  • 西藏
  • 新疆
  • 宁夏
  • 兵团
手机号立即预约

请填写图片验证码后获取短信验证码

看不清楚,换张图片

免费获取短信验证码

Python string compar

up vote484down votefavorite
150

I've got a Python program where two variables are set to the value 'public'. In a conditional expression I have the comparison var1 is var2 which fails, but if I change it to var1 == var2 it returns True.

Now if I open my Python interpreter and do the same "is" comparison, it succeeds.

>>> s1 = 'public'
>>> s2 = 'public'
>>> s2 is s1
True

What am I missing here?

shareimprove this question
 
87  
I don't know anything about Python, but is it possible that one is comparing values while the other is comparing objects? – Cᴏʀʏ Oct 1 '09 at 15:45
6  
see: stackoverflow.com/questions/1392433/… – Nick Dandoulakis Oct 1 '09 at 15:50
 
possible duplicate of Is there a difference between `==` and `is` in Python? – AlexKM Dec 1 '14 at 13:29

is is identity testing, == is equality testing. what happens in your code would be emulated in the interpreter like this:

>>> a = 'pub'
>>> b = ''.join(['p', 'u', 'b'])
>>> a == b
True
>>> a is b
False

so, no wonder they're not the same, right?

In other words: is is the id(a) == id(b)

shareimprove this answer
 
10  
ahh same as eq? vs equal? in scheme, got it. – jottos Oct 1 '09 at 17:00
4  
There's a lot of equality predicates in Common Lisp, besides the basic hierarchy of eq, eql, equal, and equalp (the longer the name there, the more things will be found equal). – David Thornley Oct 1 '09 at 19:43
12  
Or == vs .equals() in Java. The best part is that the Python == is not analogous to the Java ==. – MatrixFrog Feb 4 '12 at 2:48
42  
+1 for including a way to create a string with the same value but as a different object. I couldn't figure one out. – Tom Dignan Jul 3 '12 at 23:57
7  
@Крайст: there is only a single None value. So it always has the same id. – SilentGhost Oct 29 '12 at 9:57

SilentGhost and others are correct here.  is is used for identity comparison, while == is used for equality comparison.

The reason this works interactively is that (most) string literals are interned by default. From Wikipedia:

Interned strings speed up string comparisons, which are sometimes a performance bottleneck in applications (such as compilers and dynamic programming language runtimes) that rely heavily on hash tables with string keys. Without interning, checking that two different strings are equal involves examining every character of both strings. This is slow for several reasons: it is inherently O(n) in the length of the strings; it typically requires reads from several regions of memory, which take time; and the reads fills up the processor cache, meaning there is less cache available for other needs. With interned strings, a simple object identity test suffices after the original intern operation; this is typically implemented as a pointer equality test, normally just a single machine instruction with no memory reference at all.

So, when you have two string literals (words that are literally typed into your program source code, surrounded by quotation marks) in your program that have the same value, the Python compiler will automatically intern the strings, making them both stored at the same memory location. (Note that this doesn't always happen, and the rules for when this happens are quite convoluted, so please don't rely on this behavior in production code!)

Since in your interactive session both strings are actually stored in the same memory location, they have the same identity, so the is operator works as expected. But if you construct a string by some other method (even if that string contains exactly the same characters), then the string may be equal, but it is not the same string -- that is, it has a different identity, because it is stored in a different place in memory.

shareimprove this answer
 
3  
Where can someone read more on the convoluted rules for when strings are interned? – Noctis SkytowerApr 11 '13 at 14:14
20  
+1 for a thorough explanation. Not sure how the other answer received so many upvotes without explaining what ACTUALLY happened. – That1Guy Apr 29 '13 at 2:07
1  
this is exactly what I thought of when I read the question. The accepted answer is short yet contains the fact, but this answer explains things far better. Nice! – Sнаđошƒаӽ Jul 19 '15 at 5:46
1  
@NoctisSkytower Googled the same and found this guilload.com/python-string-interning – Wordzilla Mar 10 at 14:41

The is keyword is a test for object identity while == is a value comparison.

If you use is, the result will be true if and only if the object is the same object. However, == will be true any time the values of the object are the same.

shareimprove this answer
 

One last thing to note, you may use the intern function to ensure that you're getting a reference to the same string:

>>> a = intern('a')
>>> a2 = intern('a')
>>> a is a2
True

As pointed out above, you should probably not be doing is to determine equality on strings. But this may be helpful to know if you have some kind of weird requirement to use is.

Note that the intern function got moved from being a built in function to being in the module sysfor Python 3.

shareimprove this answer
 

This is a side note, but in idiomatic python, you will often see things like:

if x is None: 
    # some clauses

This is safe, because there is guaranteed to be one instance of the Null Object (i.e., None).

shareimprove this answer
 

If you're not sure what you're doing, use the '=='. If you have a little more knowledge about it you can use 'is' for known objects like 'None'.

Otherwise you'll end up wondering why things doesn't work and why this happens:

>>> a = 1
>>> b = 1
>>> b is a
True
>>> a = 6000
>>> b = 6000
>>> b is a
False

I'm not even sure if some things are guaranteed to stay the same between different python versions/implementations.

shareimprove this answer
 
1  
Interesting example showing how reassigning ints makes triggers this condition. Why did this fail? Is it due to intern-ing or something else? – Paul Dec 22 '14 at 16:49
 
It looks like the reason the is returns false may due to the interpreter implementation: stackoverflow.com/questions/132988/… – Paul Dec 22 '14 at 16:55
 
Read : stackoverflow.com/questions/306313/… and  stackoverflow.com/questions/11476190/why-0-6-is-6-false – Archit Jain Dec 26 '15 at 13:04 
 
@ArchitJain Yes, those links explain it pretty well. When you read them, you'll know what numbers you can use 'is' on. I just wish they would explain why it is still not a good idea to do that :) You knowing this does not make it a good idea to assume everyone else does as well (or that the internalized number range will never change) – Mattias Nilsson Dec 29 '15 at 13:32 

is is identity testing, == is equality testing. What this means is that is is a way to check whether two things are the same things, or just equivalent. 

Say you've got a simple person object. If it is named 'Jack' and is '23' years old, it's equivalent to another 23yr old Jack, but its not the same person.

class Person(object):
   def __init__(self, name, age):
       self.name = name
       self.age = age

   def __eq__(self, other):
       return self.name == other.name and self.age == other.age

jack1 = Person('Jack', 23)
jack2 = Person('Jack', 23)

jack1 == jack2 #True
jack1 is jack2 #False

They're the same age, but they're not the same instance of person. A string might be equivalent to another, but it's not the same object.

shareimprove this answer
 
1  
@Veky I just fixed that in my edit. – Morgan 'Venti' Thrappuccino Mar 4 at 21:04

From my limited experience with python, is is used to compare two objects to see if they are the same object as opposed to two different objects with the same value.  == is used to determine if the values are identical. 

Here is a good example:

>>> s1 = u'public'
>>> s2 = 'public'
>>> s1 is s2
False
>>> s1 == s2
True

s1 is a unicode string, and s2 is a normal string. They are not the same type, but are the same value.

shareimprove this answer
 
 
Your example still follows the older text.... – jprete Oct 2 '09 at 16:16

I think it has to do with the fact that, when the 'is' comparison evaluates to false, two distinct objects are used. If it evaluates to true, that means internally it's using the same exact object and not creating a new one, possibly because you created them within a fraction of 2 or so seconds and because there isn't a large time gap in between it's optimized and uses the same object.

This is why you should be using the equality operator ==, not is, to compare the value of a string object.

>>> s = 'one'
>>> s2 = 'two'
>>> s is s2
False
>>> s2 = s2.replace('two', 'one')
>>> s2
'one'
>>> s2 is s
False
>>>

In this example, I made s2, which was a different string object previously equal to 'one' but it is not the same object as s, because the interpreter did not use the same object as I did not initially assign it to 'one', if I had it would have made them the same object.

shareimprove this answer
 
1  
Using .replace() as an example in this context is probably not the best, though, because its semantics can be confusing.  s2 = s2.replace() will always create a new string object, assign the new string object to s2, and then dispose of the string object that s2 used to point to. So even if you did s = s.replace('one', 'one') you would still get a new string object. – Daniel Pryden Oct 1 '09 at 16:20
 
Ah, good point and that is true. – meder omuraliev Oct 1 '09 at 16:46

I believe that this is known as "interned" strings. Python does this, so does Java, and so do C and C++ when compiling in optimized modes.

If you use two identical strings, instead of wasting memory by creating two string objects, all interned strings with the same contents point to the same memory.

This results in the Python "is" operator returning True because two strings with the same contents are pointing at the same string object. This will also happen in Java and in C.

This is only useful for memory savings though. You cannot rely on it to test for string equality, because the various interpreters and compilers and JIT engines cannot always do it.

shareimprove this answer
 

I am answering the question even though the question is to old because no answers above quotes the language reference

Actually the is operator checks for identity and == operator checks for equality,

From Language Reference:

Types affect almost all aspects of object behavior. Even the importance of object identity is affected in some sense: for immutable types, operations that compute new values may actually return a reference to any existing object with the same type and value, while for mutable objects this is not allowed. E.g., after a = 1; b = 1, a and b may or may not refer to the same object with the value one, depending on the implementation, but after c = []; d = [], c and d are guaranteed to refer to two different, unique, newly created empty lists. (Note that c = d = [] assigns the same object to both c and d.)

so from above statement we can infer that the strings which is an immutable type may fail when checked with "is" and may checked succeed when checked with "is"

The same applies for int,tuple which are also immutable types

shareimprove this answer
 

Your Answer

Python string compar

下载Word文档到电脑,方便收藏和打印~

下载Word文档

猜你喜欢

Python string compar

up vote484down votefavorite150I've got a Python program where two variables are set to the value 'public'. In a conditio
2023-01-31

python string

方法 描述string.ljust(width)返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串\>>> x.ljust(20)'Hello,wold ‘string.center(width)返回一个原字
2023-01-31

python 2to3 string b

在xlrd sheet.lib里面,有这么一段def unpack_RK(rk_str): flags = ord(rk_str[0]) if flags & 2: # There's a SIGNED 30-bi
2023-01-31

String操作篇-python

#!/usr/bin/python # -*- coding: utf-8 -*- _str="Hello,World" #1.获取字符串的第一个字母 print "1.获取字符串的第一个字母:"+_str[0] #2.获取字符串的第二和第
2023-01-31

python string类方法

!/usr/bin/env python-*- coding: utf-8 -*-name = "app"t = name.capitalize() #首字母大写print(t)name = "app"t = name.center(20
2023-01-31

python string与list互转

因为python的read和write方法的操作对象都是string。而操作二进制的时候会把string转换成list进行解析,解析后重新写入文件的时候,还得转换成string。>>> import string>>> str = 'abc
2023-01-31

关于python的doc string

PEP 257 (http://www.python.org/peps/pep-0257.html) 定义了 doc string规范。Python Style Guide (http://www.python.org/doc/essays
2023-01-31

python基础操作---string

1 #coding:utf-8 2 3 var1 = 'Hello World!' 4 5 print var1[::] 6 print len(var1) 7 print var1[0:len(var1)] 8 print var1[
2023-01-31

Python小技巧 - string 和

本文记录了 str 转 list 的多种情况的解决方法和 list 转 str 的解决方法。目录1. string to list2. list to string1. string to list情况1:'[1,2,3,4]' ->
2023-01-31

Python string中删除(过滤)

最近做了一个需求,把公众号的用户信息同步到服务端,发现很多用户的昵称里面都有表情符号(emoji), 一般的处理方式是把MySQL的编码改成 utf8mb4,后来讨论了下,这些表情也没什么用,入库的时候直接删除就好了。过滤方法Python
2023-01-31

Python基础-int和string互

int转成string,函数int(string)string转成int,函数str(number)如下:
2023-01-31

python中string和bool的转

python中字符串“True” 和 “False"转为bool类型时, 不能通过bool(xx)强转。注意是因为在python中,除了‘’、""、0、()、[]、{}、None为False, 其他转换都为True。 也就是说字符串如果不为
2023-01-31

Python之string编码问题

这篇文章主要介绍了Python之string编码问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
2023-02-28

Python - 定制语法的string

定制语法的string模板(template) 详解本文地址: http://blog.csdn.net/caroline_wendy/article/details/28614491string模板(template)参考: http:/
2023-01-31

python字符串String模块

python的string模块1.字符串属性方法操作:1.>字符串格式输出对齐>>> str = "Python stRING"  >>> print str.center(20)           #生成20个字符长度,str排中间 
2023-01-31

python之string模块的find

函数原型:find(str,pos_start,pos_end)解释:str:被查找“字串”(气味字符串的函数);pos_start:查找的首字母位置(从0开始计数。默认:0);pos_end:查找的末 尾位置(不包括末尾位置。默认-1)返
2023-01-31

Python基础:字符串(string)

字符串的常用操作  字符串与数组一样,支持索引操作、切片与遍历  索引、切片操作:name = 'jason'name[0]'j'name[1:3]'as'  遍历:for char in name: print(char) ja
2023-01-31

ORA-48001: internal error code, arguments: [string], [string], [string], [string], [string], [string

文档解释ORA-48001: internal error code, arguments: [string], [string], [string], [string], [string], [string], [string],
ORA-48001: internal error code, arguments: [string], [string], [string], [string], [string], [string
2023-11-05

ORA-00600: internal error code, arguments: [string], [string], [string], [string], [string], [string

文档解释ORA-00600: internal error code, arguments: [string], [string], [string], [string], [string], [string], [string],
ORA-00600: internal error code, arguments: [string], [string], [string], [string], [string], [string
2023-11-05

编程热搜

  • Python 学习之路 - Python
    一、安装Python34Windows在Python官网(https://www.python.org/downloads/)下载安装包并安装。Python的默认安装路径是:C:\Python34配置环境变量:【右键计算机】--》【属性】-
    Python 学习之路 - Python
  • chatgpt的中文全称是什么
    chatgpt的中文全称是生成型预训练变换模型。ChatGPT是什么ChatGPT是美国人工智能研究实验室OpenAI开发的一种全新聊天机器人模型,它能够通过学习和理解人类的语言来进行对话,还能根据聊天的上下文进行互动,并协助人类完成一系列
    chatgpt的中文全称是什么
  • C/C++中extern函数使用详解
  • C/C++可变参数的使用
    可变参数的使用方法远远不止以下几种,不过在C,C++中使用可变参数时要小心,在使用printf()等函数时传入的参数个数一定不能比前面的格式化字符串中的’%’符号个数少,否则会产生访问越界,运气不好的话还会导致程序崩溃
    C/C++可变参数的使用
  • css样式文件该放在哪里
  • php中数组下标必须是连续的吗
  • Python 3 教程
    Python 3 教程 Python 的 3.0 版本,常被称为 Python 3000,或简称 Py3k。相对于 Python 的早期版本,这是一个较大的升级。为了不带入过多的累赘,Python 3.0 在设计的时候没有考虑向下兼容。 Python
    Python 3 教程
  • Python pip包管理
    一、前言    在Python中, 安装第三方模块是通过 setuptools 这个工具完成的。 Python有两个封装了 setuptools的包管理工具: easy_install  和  pip , 目前官方推荐使用 pip。    
    Python pip包管理
  • ubuntu如何重新编译内核
  • 改善Java代码之慎用java动态编译

目录