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.
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.