String resident mechanism and string comparison (string)

String resides:

Only save a copy of the same immutable string, different values ​​are stored in the string resident pool, Python points out the string resident mechanism, for strings that conform to the identifier rules (only underscore (_), Letters and numbers) will enable the string resident mechanism

For strings. Three factors need to be judged: identification (ID), type (Type), and value (Value)

>>> a= "abd_33"
>>> b= "abd_33"
>>> a is b
True
>>> c="dd#"
>>> d="dd#"
>>> c is d
False
>>> str1="aa"
>>> str2="bb"
>>> str1+str2 is "aabb"
False
>>> str1+str2 == "aabb"
True
>>> id(a)
2751784948432
>>> id(b)
2751784948432
>>> id(c)
2751784950056
>>> id(d)
2751784950112

Comparison and identity
of character strings : For the three elements of character strings: ID, Type, and Value, when comparing character strings, they will be compared based on different elements of characters.
is used to compare the identification (ID) of the string identifier, == or != is the comparison of string values

>>> a= "hello"
>>> b= "world"
>>> c= "helloworld"
>>> c is (a + b)
False
>>> c == (a + b)
True

Member operator
in/not in. Used to determine whether a character (substring) exists in the string

>>> d = "helloworld"
>>> e = "h"
>>> f="or"
>>> e in d
True
>>> f in d
True
>>> f not in d
False

Guess you like

Origin blog.csdn.net/yue008/article/details/108594317