How do I check if a string is a valid keyword in Python?



In programming, the keyword — it is a “ reserved word ” of the language that gives the interpreter a special meaning . It can be a command or a parameter. The keywords cannot be used as a variable name in a program snippet.

Keywords in Python: Python also reserves some keywords that convey special meaning. Knowing this is a necessary part of learning this language. Below is a list of the keywords registered by Python. 

Lies, elif, lambda,
Nobody else, non-local,
True, except
and finally, or,
as, for pass,
assert, from, raise,
break, global, return,
class, if try,
continue, import, bye,
def, in, with ,
Del, yes, profitability,

How to check if a string is a keyword?

Python defines in its own language a built-in “ keyword ” module that handles certain operations related to keywords. The iskeyword () function checks if a string is a keyword or not. Returns true if the string is a keyword, otherwise returns false .

# Python code to demonstrate how iskeyword () works

 
# import & quot; keyword & quot; for operations with keywords

import keyword

 
# initialize strings for testing

s = "for"

s1 = "pythonengineering"

s2 = "elif"

s3 = "elseif"

s4 = " nikhil "

s5 = "assert"

s6 = "shambhavi"

s7  = "True"

s8 = "False"

s9 = "akshat"

s10 = "akash"

s11 = "break"

s12 = " ashty "

s13 = "lambda"

s14 = "suman"

s15 = "try"

s16 = "vaishn avi "

  
# check which keywords

if keyword.iskeyword (s):

print (s + "is a python keyword" )

else print (s + "is not a python keyword" )

 

if keyword.iskeyword (s1):

print (s1 + "is ap ython keyword " )

else print (s1 + "is not a python keyword" )

 

if keyword.iskeyword (s2):

print (s2 + " is a python keyword " )

else print (s2 + "is not a python keyword" )

 

if keyword.iskeyword (s3):

print (s3 + "is a python keyword" )

else print (s3 + "is not a python keyword" )

 

if keyword.iskeyword (s4):

print (s4 + "is a python keyword" )

else print (s4 + "is not a python keyword" )

 

if keyword.iskeyword (s5):

print (s5 + "is a python keyword" )

else print (s5 + "is not a python keyword" )

 

if keyword.iskeyword (s6):

print (s6 + "is a python keyword" )

else print (s6 + "is not a python keyword" )

 

if keyword.iskeyword ( s7):

print (s7 + "is a python keyword" )

else print (s7 + " is not a python keyword " )

  

if keyword.iskeyword (s8):

print (s8 + "is a python keyword" )

else print (s8 + "is not a python keyword" )

 

if keyword.iskeyword (s9):

print (s9 + "is a python keyword" )

else print (s9 + " is not a python keyword " )

  

if keyword.iskeyword (s10):

  print (s10 + " is a python keyword " )

else print (s10 + "is not a python keyword" )

  

if keyword.iskeyword (s11):

print (s11 + " is a python keyword " )

else print (s11 + "is not a python keyword " )

  

if keyword.iskeyword (s12):

print (s12 + "is a python keyword " )

else print (s12 + " is not a python keyword "  )

 

if keyword.iskeyword (s13):

  print (s13 + "is a python keyword" )

else print (s13 + "is not a python keyword " )

  

if keyword.iskeyword (s14):

  print (s14 + " is a python keyword " )

else print (s14 + "is not a python keyword" )

 

if keyword.iskeyword (s15):

print (s15 + "is a python keyword" )

else print (s15 + "is not a python keyword " )

  

if keyword.isk eyword (s16):

print (s16 + "is a python keyword" )

else print (s16 + "is not a python keyword" )

Output:

 for is a python keyword pythonengineering is not a python keyword elif is a python keyword elseif is not a python keyword nikhil is not a python keyword assert is a python keyword shambhavi is not a python keyword True is a python keyword False is a python keyword akshat is not a python keyword akash is not a python keyword break is a python keyword ashty is not a python keyword lambda is a python keyword suman is not a python keyword tr y is a python keyword vaishnavi is not a python keyword 

How to print a list of all keywords

Sometimes memorizing all the keywords can be tricky when assigning variable names. Therefore, the module "keyword" provides a function " kwlist () " which prints all 33 python keywords .

# Python code to demonstrate how iskeyword () works

  
# import "keyword" for operations with keywords

import keyword

 
# print all keywords at once using & quot; kwlist () & quot;

print ( "The list of keywords is:" )

print (keyword.kwlist)

Output:

 The list of keywords is: [`False`, `None`,` True`, `and`,` as`, `assert`,` break`, `class`,` continue`, `def`,` del`, `elif`,` else`, `except `,` finally`, `for`,` from`, `global`,` if`, `import`,` in`, `is`,` lambda`, `nonlocal`,` not`, `or`, `pass`,` raise`, `return`,` try`, `while`,` with`, `yield`] 

Following articles:

This article courtesy of Manjeet Singh (C .Nandini) . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.