python3数据类型转换


python3 列表转 元组

list2 = [2,w]
tuple2 = tuple(list2)

python3 元组转 字典

tuple2 = ((2, “w”),(3, “e”))

dict2 = dict(tuple2)

dict2 = dict((y, x) for x, y in tuple2) 元组x、y调换位置后转字典

ASCII码与字符的相互转换

ord()将ASCII字符转换为对应的数值

  1. s="hello world"
  2. for i in s:
  3. print(ord(i),end=" ")
  4. # 104 101 108 108 111 32 119 111 114 108 100
  5. ord('A')
  6. # 65

chr()将数值转换为对应的ASCII字符

  1. chr(65)
  2. # 'A'

local 2021年10月21日 09:39 收藏文档