The Wayback Machine - https://web.archive.org/web/20240108212022/https://www.geeksforgeeks.org/check-string-valid-keyword-python/
Open In App
Related Articles

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In programming, a keyword is a “reserved word” by the language that conveys special meaning to the interpreter. It may be a command or a parameter. Keywords cannot be used as a variable name in the program snippet.

What is Keywords in Python

Python also reserves some keywords that convey special meaning. Knowledge of these is a necessary part of learning this language. Below is a list of keywords registered by Python. 

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

Python Program to Check if a String is a Keyword

Python in its language defines an inbuilt module keyword that handles certain operations related to keywords. iskeyword() checks if a string is a keyword or not. Returns true if a string is a keyword, else returns false.

Python3




# importing "keyword" for keyword operations
import keyword
# initializing strings for testing while putting them in an array
keys = ["for", "geeksforgeeks", "elif", "elseif", "nikhil",
        "assert", "shambhavi", "True", "False", "akshat",
        "akash", "break", "ashty", "lambda", "suman",
        "try", "vaishnavi"]
 
for i in range(len(keys)):
     # checking which are keywords
    if keyword.iskeyword(keys[i]):
        print(keys[i] + " is python keyword")
    else:
        print(keys[i] + " is not a python keyword")


Output

for is python keyword
geeksforgeeks is not a python keyword
elif is python keyword
elseif is not a python keyword
nikhil is not a python keyword
assert is python keyword
shambhavi is not a python keyw...

Print a list of all keywords

Sometimes, remembering all the keywords can be a difficult task while assigning variable names. Hence, kwlist() function is provided in the keyword module which prints all the 33 python keywords.

Python3




# 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 : 
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda...

Next Articles:  


Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 25 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials