如何检查给定的单词是否是Python关键字?
在此程序中,我们将检查给定的单词是否为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