python的keyword模块用法实例分析

yipeiwu_com6年前Python基础

本文实例讲述了python的keyword模块用法。分享给大家供大家参考。具体如下:

Help on module keyword:
NAME
  keyword - Keywords (from "graminit.c")
FILE
  /usr/lib64/python2.6/keyword.py
DESCRIPTION
  This file is automatically generated; please don't muck it up!
  To update the symbols in this file, 'cd' to the top directory of
  the python source tree after building the interpreter and run:
    python Lib/keyword.py
FUNCTIONS
  iskeyword = __contains__(...)
    x.__contains__(y) <==> y in x.
DATA
  __all__ = ['iskeyword', 'kwlist']
  kwlist = ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', ...

得到python的关键字列表:

>>> keyword.kwlist
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

判断字符串是否是python的关键字

>>> keyword.iskeyword('and')
True
>>> 
>>> keyword.iskeyword('has')
False

希望本文所述对大家的Python程序设计有所帮助。

相关文章

python基于queue和threading实现多线程下载实例

本文实例讲述了python基于queue和threading实现多线程下载的方法,分享给大家供大家参考。具体方法如下: 主代码如下: #download worker qu...

python 利用栈和队列模拟递归的过程

一、递归 递归调用:一个函数,调用的自身,称为递归调用 递归函数:一个可以调用自身的函数称为递归函数   凡是循环能干的事,递归都能干 方法: 1、写出临界条件 2、找这一次和上一次的关...

python日期相关操作实例小结

本文实例讲述了python日期相关操作。分享给大家供大家参考,具体如下: 用 Python 做项目时,经常会遇到与日期转换相关,日期计算相关的功能,动不动就要去查python手册,感觉麻...

解决Python print输出不换行没空格的问题

今天在做编程题的时候发现Python的print输出默认换行输出,并且输出后有空格。 题目要求输出 122 而我的输出是: 1 2 2 于是我百度查到取消print自动换行的方法:就是在...

python如何去除字符串中不想要的字符

问题:     过滤用户输入中前后多余的空白字符       ‘    ++++abc123---    ‘     过滤某w...