How to check if a string is a valid keyword in Python?
Last Updated :
25 Nov, 2023
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")
Outputfor 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)
OutputThe 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:
Similar Reads
How to check a valid regex string using Python? A Regex (Regular Expression) is a sequence of characters used for defining a pattern. This pattern could be used for searching, replacing and other operations. Regex is extensively utilized in applications that require input validation, Password validation, Pattern Recognition, search and replace ut
6 min read
Check if a given string is binary string or not - Python The task of checking whether a given string is a binary string in Python involves verifying that the string contains only the characters '0' and '1'. A binary string is one that is composed solely of these two digits and no other characters are allowed. For example, the string "101010" is a valid bi
3 min read
How to check if an object is iterable in Python? In simple words, any object that could be looped over is iterable. For instance, a list object is iterable and so is an str object. The list numbers and string names are iterables because we are able to loop over them (using a for-loop in this case). In this article, we are going to see how to check
3 min read
Python - Check if substring present in string The task is to check if a specific substring is present within a larger string. Python offers several methods to perform this check, from simple string methods to more advanced techniques. In this article, we'll explore these different methods to efficiently perform this check.Using in operatorThis
2 min read
Check If String is Integer in Python In this article, we will explore different possible ways through which we can check if a string is an integer or not. We will explore different methods and see how each method works with a clear understanding.Example:Input2 : "geeksforgeeks"Output2 : geeksforgeeks is not an IntigerExplanation : "gee
4 min read