{"id":16578,"date":"2021-08-12T09:25:07","date_gmt":"2021-08-12T03:55:07","guid":{"rendered":"https:\/\/python-programs.com\/?p=16578"},"modified":"2021-11-22T18:38:33","modified_gmt":"2021-11-22T13:08:33","slug":"python-program-to-check-if-a-string-is-a-keyword-or-not","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-if-a-string-is-a-keyword-or-not\/","title":{"rendered":"Python Program to Check if a String is a keyword or Not"},"content":{"rendered":"

In the previous article, we have discussed Python Program to Remove Elements from a Tuple<\/a>
\nDefinition of Keyword:\u2013<\/strong><\/p>\n

A keyword is a reserved word in programming languages that has its own unique meaning. It conveys their unique meaning to the interpreter while executing. And, when using variables in code, we never use the keyword as the variable name.<\/p>\n

Some of the keywords in Python are :<\/strong><\/p>\n

True, False, finally, not, or, and, if, else, elif, None, lambda, nonlocal, not, except, as, pass, try, def, in, with, while, import, continue, from, raise, return, global, class, break, from, assert, for, in, with, is, yield, del, and so on.<\/p>\n

kwlist method:<\/strong><\/p>\n

To accomplish this, we must import a built-in Python module called “keyword,” and within the keyword module, there is a method called “kwlist” that stores all of the keywords found in the Python language in a list. And if the given string appears in the list, it is considered a keyword; otherwise, it is not a keyword.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given First String = btechgeeks\r\nGiven Second String = for<\/pre>\n

Output:<\/strong><\/p>\n

The given string{ btechgeeks } is not a keyword\r\nThe given string{ for } is a keyword<\/pre>\n

Example 2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given First String = while\r\nGiven Second String = for<\/pre>\n

Output:<\/strong><\/p>\n

The given string{ while } is a keyword\r\nThe given string{ for } is a keyword<\/pre>\n

Program to Check if a String is a keyword or Not<\/h2>\n

Below are the ways to check if a String is a keyword or Not.<\/p>\n