Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I'm making a set in Python to house entire symbols on my keyboard, yet clearly, a couple of representing a few issues. Is there an approach to get them all in there without experiencing issues? 

Look at my set:

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

To get around remarking out its vast majority, since in Python # is to comment, I encased everything like so: 

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

This works for that character, however now I would already be able to see an issue when I run over the ' and \. Is there a superior method to make this set?

1 Answer

0 votes
by (26.4k points)

You can actually fix the backslash punctuation line by getting away from it and ' can be fixed by placing it in double-quotes: 

symbols = {..., '\\', ... "'", ...}

However, composing this out is really dreary. Why not simply use string.punctuation all things being equal:

>>> from string import punctuation

>>> set(punctuation)

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

>>>

Wanna become a Python expert? Come and join the python certification course and get certified.

For more details, do check out the below video tutorial...

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 29, 2019 in Java by Suresh (3.4k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...