Python 3 - String
Updated: 2020-12-31
String Interning
Strings will be interned, saved as one object, so is
is returning True
>>> S1 = 'spam'
>>> S2 = 'spam'
>>> S1 == S2, S1 is S2
(True, True)
Another example
>>> S1 = 'alonglongstring'
>>> S2 = 'alonglongstring'
>>> S1 == S2, S1 is S2
(True, True)
String with empty space, is
is returning False
>>> S1 = 'a longer string'
>>> S2 = 'a longer string'
>>> S1 == S2, S1 is S2
(True, False)
MakeTrans
str.maketrans()
bytes.maketrans()
bytearray.maketrans(from, to)
String Format
>>> "hello {}".format("world")
'hello world'
>>> "{} {}".format("hello", "world")
'hello world'