How to include backslash and quotes in Python strings

前端 未结 4 1049
故里飘歌
故里飘歌 2021-01-27 00:41

I\'ve got an array of special characters that looks something like this.

specialCharList=[\'`\',\'~\',\'!\',\'@\',\'#\',\'$\',\'%\',\'^\',
             \'&\'         


        
4条回答
  •  [愿得一人]
    2021-01-27 01:19

    The readability of your list could be greatly improved by putting your special characters in a string:

    >>> SCL = "`~!@#$%^&*()_-+=|{}[],;:'.>>> specialCharacters = list(SCL)
    

    We're combining two strings, one delimited by " and where we put ', the second delimited by ' and where we put " and \\ (that we have to escape, so we have to put '\\').

提交回复
热议问题