Python program to check if a given string is Keyword or not


In this article, we will learn about the solution to the problem statement given below.

Problem statement − We are given a number, we need to check that the number is a power of two or not.

Keywords are the special words reserved by any language with specific usage and cannot be used as an identifier.

To check whether the given string is a keyword we used the keyword module as discussed below.

Example

 Live Demo

# keyword module
import keyword
# Function
def isKeyword(word) :
   # list of all keywords
   keyword_list = keyword.kwlist
   # check the presence
   if word in keyword_list :
      return "Yes"
   else :
      return "No"
# main
if __name__ == "__main__" :
   print(isKeyword("tut"))
   print(isKeyword("or"))

Output

No
Yes

Conclusion

In this article, we have learned how we can check the string is a keyword or not.

Updated on: 23-Dec-2019

290 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements