如何檢查字串是否是Python中的有效關鍵字?

藏色散人
發布: 2019-03-20 14:16:30
原創
3339 人瀏覽過

在程式設計中,關鍵字(keyword )是語言的“reserved word”,它會向解譯器傳遞特殊的意義。它可以是命令或參數。關鍵字不能在程式段中用作變數名。

如何檢查字串是否是Python中的有效關鍵字?

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 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 keyword
geeksforgeeks is not a python keyword
elif is a python keyword
elseif is not a python keyword
nikhil is not a python keyword
assert is a python keyword
shambhavi is not a python keyword
True is a python keyword
False is a python keyword
akshat is not a python keyword
akash is not a python keyword
break is a python keyword
ashty is not a python keyword
lambda is a python keyword
suman is not a python keyword
try is a python keyword
vaishnavi 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']
登入後複製

相關推薦:《Python教程

#本篇文章就是關於檢查字串是否是Python中的有效關鍵字的方法介紹,希望對需要的朋友有幫助!

以上是如何檢查字串是否是Python中的有效關鍵字?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!