def encode_string(data, encoding='hex'):
    '''
    Encode string

    :param data: string to encode
    :param encoding: encoding to use (default: 'hex')
    :return: encoded string
    '''
    if six.PY2:
        return data.encode(encoding)
    else:
        if isinstance(data, str):
            data = bytes(data, 'utf-8')
        return codecs.encode(data, encoding).decode('ascii')
Encode string

    :param data: string to encode
    :param encoding: encoding to use (default: 'hex')
    :return: encoded string