Intellipaat Back

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

Hello. How can I get a list of all python keywords as strings? 

Similar to this code:

import syntax

print syntax.keywords

# prints ['print', 'if', 'for', etc...]

1 Answer

0 votes
by (26.4k points)

If you want to look at the entire keywords, They're all listed in a module called keyword module.

>>> import keyword

>>> keyword.kwlist

['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif',

 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import',

 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try',

 'while', 'with', 'yield']

Click on this link, if you want to know more about keyword.kwlist 

Want to learn more about Python? Come and Join: Python course

Related questions

0 votes
1 answer
asked Oct 8, 2019 in Python by Sammy (47.6k points)
+1 vote
2 answers
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...