函数接收一个字符串s,要求返回一个元组。元组中第一个元素是s使用UTF8编码之后的字节串。元组中第二个元素是s使用gbk编码之后的字符串。如果参数s不是字符串,返回参数"必须为字符串"。
def fun(s):
if isinstance(s, str):
return (s.encode('utf8'), s.encode('gbk'))
else:
return '必须为字符串'
print(fun('你好'))
输出
(b'\xe4\xbd\xa0\xe5\xa5\xbd', b'\xc4\xe3\xba\xc3')
def func_x(s):
if isinstance(s, str):
return (s.encode().decode('utf8'), s.encode().decode('gbk'))
else:
return '参数必须为字符串'