如何连接"\"和字符串以获取 unicode 字符



我想获得一个字典,其中包含Unicode中该范围的字符,但无法连接"\"one_answers"u0061",例如

for i in range(97, 123):
dict[('\u00' + (hex(i)[2:]))] = ''


'u00' + '61' #does not work because after 'u' required 4 symbols
r'u00' + '61' #returns '\u0061' instead of 'a'
'\u0061'[1:]  # slices both "\"

要从int中获取相应的字符,请使用内置的chr函数:

>>> chr(101)
'e'
>>> chr(0x1F600)
'😀'

然而,您似乎想要对所有小写字母进行迭代。来自string模块的常数ascii_lowercase更适合于此目的。

import string
dct = {} # don't use 'dict' as a variable name, it shadows the dict constructor
for c in string.ascii_lowercase:
dct[c] = ''

最新更新