def to_string ( s , encoding = 'utf-8' ) : if six . PY2 : return s . encode ( encoding ) if isinstance ( s , bytes ) : return s . decode ( encoding ) return s
python3 string default encoding
Accept unicode ( py2 ) or bytes ( py3 )
def to_string(s, encoding='utf-8'):
    """
    Accept unicode(py2) or bytes(py3)

    Returns:
        py2 type: str
        py3 type: str
    """
    if six.PY2:
        return s.encode(encoding)
    if isinstance(s, bytes):
        return s.decode(encoding)
    return s