How to check if a string is a valid keyword in Python?

Keyword:

It is a reserved word that conveys special meaning to the interpreter. A keyword can be a command or a parameter. While choosing variable names, we should keep in mind that keywords cannot be chosen as variable names.

Keywords in Python Language:

Python language also houses many keywords that should be known in order to understand the language well. Below are the keywords which are present in Python.

False, elif, lambda,

True, except, not,

and, finally, or,

None, else, nonlocal,

del, is, yield,

assert, from, raise,

as, for, pass,

break, global, return,

class, if, try,

continue, import, while,

def, in, with

 

How to check if a string is a keyword?

Python houses an inbuilt module “keyword” in its programming language. This module takes care of certain operations relating to keywords. In order to check whether a certain string is a keyword or not “iskeyword()” function is used. It returns True for a keyword or else returns False.

 

#Demonstrating working of iskeyword()

# importing “keyword” for keyword operations

import keyword

 

# initializing strings for testing

s = “for”

s1 = “pstanalytics”

s2 = “elif”

s3 = “elseif”

s4 = “sachin”

s5 = “assert”

s6 = “mahadev”

s7 = “True”

s8 = “False”

s9 = “surbhi”

s10 = “gopal”

s11 = “break”

s12 = “mrinal”

s13 = “lambda”

s14 = “ajay”

s15 = “try”

s16 = “vishal”

 

# checking which are keywords

ifkeyword.iskeyword(s):

                        print ( s + “belongs to python keyword”)

else : print ( s + “does not belong to python keyword”)

 

ifkeyword.iskeyword(s1):

                        print ( s1 + “ belongs to python keyword”)

else : print ( s1 + “ does not belong to python keyword”)

 

ifkeyword.iskeyword(s2):

                        print ( s2 + “ belongs to python keyword”)

else : print ( s2 + “ does not belong to python keyword”)

 

ifkeyword.iskeyword(s3):

                        print ( s3 + “ belongs to python keyword”)

else : print ( s3 + “ does not belong to python keyword”)

 

ifkeyword.iskeyword(s4):

                        print ( s4 + “ belongs to python keyword”)

else : print ( s4 + “ does not belong to python keyword”)

 

ifkeyword.iskeyword(s5):

                        print ( s5 + “ belongs to python keyword”)

else : print ( s5 + “ does not belong to python keyword”)

 

ifkeyword.iskeyword(s6):

                        print ( s6 + “ belongs to python keyword”)

else : print ( s6 + “ does not belong to python keyword”)

 

ifkeyword.iskeyword(s7):

                        print ( s7 + “ belongs to python keyword”)

else : print ( s7 + “ does not belong to python keyword”)

 

ifkeyword.iskeyword(s8):

                        print ( s8 + “ belongs to python keyword”)

else : print ( s8 + “ does not belong to python keyword”)

 

ifkeyword.iskeyword(s9):

                        print ( s9 + “ belongs to python keyword”)

else : print ( s9 + “ does not belong to python keyword”)

 

ifkeyword.iskeyword(s10):

                        print ( s10 + “ belongs to python keyword”)

else : print ( s10 + “ does not belong to python keyword”)

 

ifkeyword.iskeyword(s11):

                        print ( s11 + “ belongs to python keyword”)

else : print ( s11 + “ does not belong to python keyword”)

 

ifkeyword.iskeyword(s12):

                        print ( s12 + “ belongs to python keyword”)

else : print ( s12 + “ does not belong to python keyword”)

 

ifkeyword.iskeyword(s13):

                        print ( s13 + “ belongs to python keyword”)

else : print ( s13 + “ does not belong to python keyword”)

 

ifkeyword.iskeyword(s14):

                        print ( s14 + “ belongs to python keyword”)

else : print ( s14 + “ does not belong to python keyword”)

 

ifkeyword.iskeyword(s15):

                        print ( s15 + “ belongs to python keyword”)

else : print ( s15 + “ does not belong to python keyword”)

 

ifkeyword.iskeyword(s16):

                        print ( s16 + “ belongs to python keyword”)

else : print ( s16 + “ does not belong to python keyword”)

 

Output:

for belongs to python keyword

pstanalytics does not belong to python keyword

elif belongs to python keyword

elseif does not belong to python keyword

sachin does not belong to python keyword

assert belongs to python keyword

mahadev does not belong to python keyword

True belongs to python keyword

False belongs to python keyword

Surbhi does not belong to python keyword

gopal does not belong to python keyword

break belongs to python keyword

mrinal does not belong to python keyword

lambda belongs to python keyword

ajay does not belong to python keyword

try belongs to python keyword

vishal does not belong to python keyword

 

Printing List of All Keywords:

Python consists of a total of 33 keywords, which makes it difficult to remember while assigning names to variables. The function “kwlist()” provided in the “keyword” module solves this problem by printing all the 33 keywords in Python.

 

#Python code to demonstrate the working of kwlist()

# importing “keyword” for keyword operations

import keyword

 

# printing all keywords at once using “kwlist()”

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’]

Leave a Reply

Your email address will not be published. Required fields are marked *