def smart_str(s, encoding='utf-8', strings_only=False, errors='strict'):
 if (strings_only and isinstance(s, (types.NoneType, int))):
  return s
 if (not isinstance(s, basestring)):
  try:
   return str(s)
  except UnicodeEncodeError:
   if isinstance(s, Exception):
    return ' '.join([smart_str(arg, encoding, strings_only, errors) for arg in s])
   return unicode(s).encode(encoding, errors)
 elif isinstance(s, unicode):
  return s.encode(encoding, errors)
 elif (s and (encoding != 'utf-8')):
  return s.decode('utf-8', errors).encode(encoding, errors)
 else:
  return s

def smart_str ( s , encoding = 'utf-8' , strings_only = False , errors = 'strict' ) : if ( strings_only and isinstance ( s , ( types . NoneType , int ) ) ) : return s if ( not isinstance ( s , basestring ) ) : try : return str ( s ) except UnicodeEncodeError : if isinstance ( s , Exception ) : return ' ' . join ( [ smart_str ( arg , encoding , strings_only , errors ) for arg in s ] ) return unicode ( s ) . encode ( encoding , errors ) elif isinstance ( s , unicode ) : return s . encode ( encoding , errors ) elif ( s and ( encoding != 'utf-8' ) ) : return s . decode ( 'utf-8' , errors ) . encode ( encoding , errors ) else : return s
adapted from django .