字符串文字中的特殊字符

Ill*_*uss 3 python string set

我在Python中创建一个用于容纳键盘上所有符号的集合,但显然有一些构成了一些问题.有没有办法让他们在那里没有遇到问题?

这是我的套装:

symbols = {`,~,!,@,#,$,%,^,&,*,(,),_,-,+,=,{,[,},},|,\,:,;,",',<,,,>,.,?,/}
Run Code Online (Sandbox Code Playgroud)

为了解决大部分内容,因为在Python #中要评论,我把所有内容包括在内:

symbols = {'`','~','!','@','#','$','%','^','&','*','(',')','_','-','+','=','{','[','}','}','|','\',':',';','"',''','<',',','>','.','?','/'}
Run Code Online (Sandbox Code Playgroud)

哪个适用于那个角色,但现在我已经看到了一个问题,当我遇到'\.是否有更好的方法来制作此套装?

iCo*_*dez 12

您可以通过转义来修复反斜杠,并'可以通过将其放在双引号中来修复:

symbols = {..., '\\', ... "'", ...}
Run Code Online (Sandbox Code Playgroud)

但输入所有这些是相当繁琐的.为什么不string.punctuation改用:

>>> from string import punctuation
>>> set(punctuation)
{'~', ':', "'", '+', '[', '\\', '@', '^', '{', '%', '(', '-', '"', '*', '|', ',', '&', '<', '`', '}', '.', '_', '=', ']', '!', '>', ';', '?', '#', '$', ')', '/'}
>>>
Run Code Online (Sandbox Code Playgroud)