Return a string encoded in the provided encoding

def force_encoding(value, encoding='utf-8'):
    """
    Return a string encoded in the provided encoding
    """
    if not isinstance(value, (str, unicode)):
        value = str(value)
    if isinstance(value, unicode):
        value = value.encode(encoding)
    elif encoding != 'utf-8':
        value = value.decode('utf-8').encode(encoding)
    return value