Pages

Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Thursday, January 1, 2015

Python: string equals ignore case








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()



Tuesday, October 21, 2014

Python: get the index of a char in a string








find and index method are available. The difference is that find returns -1 when what you're looking for isn't found, index throws an exception.
s='abc'
print(s.index('b')) # 1
print(s.find('b'))  # 1
print(s.find('d'))  # -1
print(s.index('d')) # exception



Python: string types in 2.x and 3.x








  • In Python 2.x, basestring is the base class for both str and unicode, while types.StringType is str. If you want to check if something is a string, use basestring. If you want to check if something is a ascii (bytestring), use str and forget about types.
  • Since Python 3.x, types not longer has StringType; str is always unicode. basestring no longer exists.



Monday, September 1, 2014

Python: int to hex string








print(hex(255))
Result:
'0xff'
hex(255)[2:]
Result:
'ff'



Friday, August 29, 2014

Python: mutable byte array as buffer








bytearray function can be used to make a mutable byte array as buffer:
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))

see also




Thursday, August 28, 2014

Python: encode integers to byte string








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)))

see also




python: print a string as hex bytes









':'.join(x.encode('hex') for x in 'Hello World!')



Executes in python CLI:
>>> ':'.join(x.encode('hex') for x in 'Hello World!')
'48:65:6c:6c:6f:20:57:6f:72:6c:64:21'



join binary strings in Python










b''.join(['abc', '\x01\x02\x03', 'efg'])




Execute it in python cli:
>>> b''.join(['abc', '\x01\x02\x03', 'efg'])
'abc\x01\x02\x03efg'



Python: get the length of a utf-8 encoded string








len(s.encode('utf-8'))



Friday, May 9, 2014

python: check if a variable is a string








import sys

if isinstance(var, basestring if sys.version_info[0]<3 else str):
    print("it is string") # var is string

see also