如何检查给定的单词是否是Python关键字?
在此程序中,我们将检查给定的单词是否为python关键字" title="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 keywordmusic is not a keyword
in is a keyword
if is a keyword
以上是 如何检查给定的单词是否是Python关键字? 的全部内容, 来源链接: utcz.com/z/338864.html