def equals_ignore_case(s1, s2):
if s1 is None and s2 is None:
return True
if s1 is None and s2 is not None:
return False
if s1 is not None and s2 is None:
return False
return s1.lower() == s2.lower()
def equals_ignore_case(s1, s2):
if s1 is None and s2 is None:
return True
if s1 is None and s2 is not None:
return False
if s1 is not None and s2 is None:
return False
return s1.lower() == s2.lower()
s='abc'
print(s.index('b')) # 1
print(s.find('b')) # 1
print(s.find('d')) # -1
print(s.index('d')) # exception
import struct
buffer = bytearray(8)
buffer[0]=1
buffer[1]=1
struct.pack_into('>ih', buffer, 2, 100, 200)
# to convert the array to str
print(bytes(buffer))
import struct
def int_to_bytes(value, size, big_endian=False):
bs = []
for i in range(size):
offset = i * 8
b = chr((value & (0xff << offset)) >> offset)
if big_endian:
bs.insert(0, b)
else:
bs.append(b)
return b''.join(bs)
def int32_to_bytes(value, big_endian=False):
return int_to_bytes(value, 4, big_endian)
def int64_to_bytes(value, big_endian=False):
return int_to_bytes(value, 8, big_endian)
if __name__ == '__main__':
# the two commands below do the same
print(repr(int64_to_bytes(65536, big_endian=True)))
print(repr(struct.pack('>Q', 65536)))
':'.join(x.encode('hex') for x in 'Hello World!')
>>> ':'.join(x.encode('hex') for x in 'Hello World!')
'48:65:6c:6c:6f:20:57:6f:72:6c:64:21'
b''.join(['abc', '\x01\x02\x03', 'efg'])
>>> b''.join(['abc', '\x01\x02\x03', 'efg']) 'abc\x01\x02\x03efg'
import sys
if isinstance(var, basestring if sys.version_info[0]<3 else str):
print("it is string") # var is string