在此程序中,我们将检查给定的单词是否为python关键字。为此,我们将使用iskeyword()关键字库中的函数。
Step 1: Import keyboard. Step 2: Check if the given string is a keyword or not.
import keyword words = ["city", "music", "in", "if"] for word in words: if keyword.iskeyword(word): print("{} is a keyword".format(word)) else: print("{} is not a keyword".format(word))输出结果
city is not a keyword music is not a keyword in is a keyword if is a keyword