如何检查字符串是否是Python中的有效关键字?
在编程中,关键字(keyword )是语言的“reserved word”,它向解释器传递特殊的含义。它可以是命令或参数。关键字不能在程序段中用作变量名。
Python中的关键字:Python语言还保留了一些表达特殊含义的关键字。这些知识是学习这门语言的必要部分。下面是python的关键字列表。
False, elif, lambda,None, else, nonlocal,True, except, not,and, finally, or,as, for, pass,assert, from, raise,break, global, return,class, if, try,continue, import, while,def, in, with,del, is, yield,
如何检查字符串是否是关键字?
Python在其语言中定义了一个内置模块“keyword”,它处理与关键字相关的某些操作。函数“iskeyword()”检查字符串是否为关键字。如果字符串是关键字,则返回true,否则返回false。
立即学习“Python免费学习笔记(深入)”;
#Python code to demonstrate working of iskeyword() # importing "keyword" for keyword operations import keyword # initializing strings for testing s = "for"s1 = "geeksforgeeks"s2 = "elif"s3 = "elseif"s4 = "nikhil"s5 = "assert"s6 = "shambhavi"s7 = "True"s8 = "False"s9 = "akshat"s10 = "akash"s11 = "break"s12 = "ashty"s13 = "lambda"s14 = "suman"s15 = "try"s16 = "vaishnavi" # checking which are keywords if keyword.iskeyword(s): print ( s + " is a python keyword") else : print ( s + " is not a python keyword") if keyword.iskeyword(s1): print ( s1 + " is a python keyword") else : print ( s1 + " is not a python keyword") if keyword.iskeyword(s2): print ( s2 + " is a python keyword") else : print ( s2 + " is not a python keyword") if keyword.iskeyword(s3): print ( s3 + " is a python keyword") else : print ( s3 + " is not a python keyword") if keyword.iskeyword(s4): print ( s4 + " is a python keyword") else : print ( s4 + " is not a python keyword") if keyword.iskeyword(s5): print ( s5 + " is a python keyword") else : print ( s5 + " is not a python keyword") if keyword.iskeyword(s6): print ( s6 + " is a python keyword") else : print ( s6 + " is not a python keyword") if keyword.iskeyword(s7): print ( s7 + " is a python keyword") else : print ( s7 + " is not a python keyword") if keyword.iskeyword(s8): print ( s8 + " is a python keyword") else : print ( s8 + " is not a python keyword") if keyword.iskeyword(s9): print ( s9 + " is a python keyword") else : print ( s9 + " is not a python keyword") if keyword.iskeyword(s10): print ( s10 + " is a python keyword") else : print ( s10 + " is not a python keyword") if keyword.iskeyword(s11): print ( s11 + " is a python keyword") else : print ( s11 + " is not a python keyword") if keyword.iskeyword(s12): print ( s12 + " is a python keyword") else : print ( s12 + " is not a python keyword") if keyword.iskeyword(s13): print ( s13 + " is a python keyword") else : print ( s13 + " is not a python keyword") if keyword.iskeyword(s14): print ( s14 + " is a python keyword") else : print ( s14 + " is not a python keyword") if keyword.iskeyword(s15): print ( s15 + " is a python keyword") else : print ( s15 + " is not a python keyword") if keyword.iskeyword(s16): print ( s16 + " is a python keyword") else : print ( s16 + " is not a python keyword")
输出:
for is a python keywordgeeksforgeeks is not a python keywordelif is a python keywordelseif is not a python keywordnikhil is not a python keywordassert is a python keywordshambhavi is not a python keywordTrue is a python keywordFalse is a python keywordakshat is not a python keywordakash is not a python keywordbreak is a python keywordashty is not a python keywordlambda is a python keywordsuman is not a python keywordtry is a python keywordvaishnavi is not a python keyword
如何打印所有关键字的列表?
有时候,在分配变量名时,记住所有的关键字可能是比较困难。因此,在“keyword”模块中提供了一个函数“kwlist()”,它打印所有33个python关键字。
#Python code to demonstrate working of iskeyword() # importing "keyword" for keyword operations import keyword # printing all keywords at once using "kwlist()" print ("The list of keywords is : ") print (keyword.kwlist)
输出:
The list of keywords is : ['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']