{"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> 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 Output:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to check if a String is a keyword or Not.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Remove Elements from a Tuple Definition of Keyword:\u2013 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 …<\/p>\n
\nDefinition of Keyword:\u2013<\/strong><\/p>\nGiven First String = btechgeeks\r\nGiven Second String = for<\/pre>\n
The given string{ btechgeeks } is not a keyword\r\nThe given string{ for } is a keyword<\/pre>\n
Given First String = while\r\nGiven Second String = for<\/pre>\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
\n
Method #1: Using kwlist Method <\/strong>(Static Input)<\/h3>\n
\n
# Import keyword module using import keyword.\r\nimport keyword\r\n# Get all the keywords in python using keyword.kwlist method and store it in a variable.\r\nkeywrds_lst = keyword.kwlist\r\n# Give the first string as static input and store it in another variable.\r\nfst_str = \"btechgeeks\"\r\n# Give the second string as static input and store it in another variable.\r\nsecnd_str = \"for\"\r\n# Check whether the given first string is present in the above keyword list or not\r\n# using if conditional statement.\r\nif fst_str in keywrds_lst:\r\n # If the given condition is True , print \"keyword\".\r\n print(\"The given string{\", fst_str, \"} is a keyword\")\r\nelse:\r\n # If the given condition is False , print \" Not keyword\" using else conditional statement.\r\n print(\"The given string{\", fst_str, \"} is not a keyword\")\r\n# Check whether the given second string is present in the above keyword or not list\r\n# using if conditional statement.\r\nif secnd_str in keywrds_lst:\r\n # If the given condition is True , print \"keyword\".\r\n print(\"The given string{\", secnd_str, \"} is a keyword\")\r\nelse:\r\n # If the given condition is False , print \" Not keyword\" using else conditional statement.\r\n print(\"The given string{\", secnd_str, \"} is not a keyword\")\r\n<\/pre>\n
The given string{ btechgeeks } is not a keyword\r\nThe given string{ for } is a keyword<\/pre>\n
Method #2: Using kwlist Method <\/strong>(User Input)<\/h3>\n
\n
# Import keyword module using import keyword.\r\nimport keyword\r\n# Get all the keywords in python using keyword.kwlist method and store it in a variable.\r\nkeywrds_lst = keyword.kwlist\r\n# Give the first string as user input using the input() function and\r\n# store it in another variable.\r\nfst_str = input(\"Enter some random string = \")\r\n# Give the second string as user input using the input() function and\r\n# store it another variable.\r\nsecnd_str = input(\"Enter some random string = \")\r\n# Check whether the given first string is present in the above keyword list or not\r\n# using if conditional statement.\r\nif fst_str in keywrds_lst:\r\n # If the given condition is True , print \"keyword\".\r\n print(\"The given string{\", fst_str, \"} is a keyword\")\r\nelse:\r\n # If the given condition is False , print \" Not keyword\" using else conditional statement.\r\n print(\"The given string{\", fst_str, \"} is not a keyword\")\r\n# Check whether the given second string is present in the above keyword or not list\r\n# using if conditional statement.\r\nif secnd_str in keywrds_lst:\r\n # If the given condition is True , print \"keyword\".\r\n print(\"The given string{\", secnd_str, \"} is a keyword\")\r\nelse:\r\n # If the given condition is False , print \" Not keyword\" using else conditional statement.\r\n print(\"The given string{\", secnd_str, \"} is not a keyword\")\r\n<\/pre>\n
Enter some random string = while\r\nEnter some random string = for\r\nThe given string{ while } is a keyword\r\nThe given string{ for } is a keyword<\/pre>\n
\n