Write a python program to check if a given string is Keyword or not?

Here is a simple program in Python to check if a given string is a Python keyword or not: 

 

import keyword

def check_keyword(string):
  if keyword.iskeyword(string):
    return True
  else:
    return False

input_string = input("Enter a string: ")
result = check_keyword(input_string)

if result:
  print(input_string + " is a Python keyword.")
else:
  print(input_string + " is not a Python keyword.")

The keyword module in Python provides a iskeyword() function that returns True if the given string is a keyword in Python and False if it is not. In this program, we take an input string from the user and pass it to the check_keyword() function. This function returns the result of the iskeyword() function and we use an if-else loop to print the result.

This program can be useful if you want to validate user input, check if a given identifier is a valid identifier in Python, or perform other string manipulations based on whether the given string is a Python keyword or not.

Submit Your Programming Assignment Details