如何检查字符串是否是Python中的有效关键字?


本文摘自php中文网,作者藏色散人,侵删。

在编程中,关键字(keyword )是语言的“reserved word”,它向解释器传递特殊的含义。它可以是命令或参数。关键字不能在程序段中用作变量名。

Python中的关键字:Python语言还保留了一些表达特殊含义的关键字。这些知识是学习这门语言的必要部分。下面是python的关键字列表。

1

2

3

4

5

6

7

8

9

10

11

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。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

#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")

输出:

阅读剩余部分

相关阅读 >>

Python列表推导式是什么

实例解析Python3.x对json的操作

Python解释器怎么下载

详解Python函数之map,filter,reduce

Python中赋值&浅拷贝&深拷贝的简单介绍(示例)

如何在一行里获取多个异常

Python docx 中文字体设置的操作方法

Python--dicom图像的研究

Python之xpath语法

Python中的pop函数和append函数详解

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...