9 幾個內建函式
阿新 • • 發佈:2018-11-04
一、 bytes()
s = "你好" bs = s.encode("UTF-8") print(bs) s1 = bs.decode("UTF-8") print(s1) bs = bytes(s, encoding="utf-8") # 把字串串編碼成UTF-8 print(bs)View Code
二 、chr()、ord()、ascii()
ord()輸入字元找編碼位置
chr()輸入位置數字找出對應的字元
ascii() 是ascii碼中的返回值 不是就返回None
print(ord('a'))#97View Codeprint(chr(999))#ϧ print(ascii('c'))#'c'
三、exec()、eval()的作用與區別
區別:
1、exec與eval語句的主要區別是,exec處理字串裡面的程式碼可執行多行,
而eval是處理字串裡面的表示式只能執行一行,eval還可以將字串變得可以操作,一般檔案操作可以用到。
2、exec沒有返回值,eval有返回值
lis = "[2,'c',77]" #字串裡面放字串單雙引號注意 new_lis = eval(lis) new_lis.append('ccc') print(new_lis)#View Code[2, 'c', 77, 'ccc'] s = '2+3+2' res = eval(s) print(res) #7 s2 = ''' def func(): print('哎呀') result = 2+3 return result func() ''' print(exec(s2)) # 哎呀 # None
complie待補充