使用关键字模块检查给定的字符串是否是关键字

In this program, a string will be given by the user and we have to check whether it is a keyword in Python or not. To do this task in Python we will use a module which name is the keyword. Before going to do this task we will see a program that will return all keywords of Python programming language.

在此程序中,用户将给出一个字符串,我们必须检查它是否是Python中的关键字。 为了在Python中完成此任务,我们将使用一个名称为关键字的模块。 在执行此任务之前,我们将看到一个程序,该程序将返回Python编程语言的所有关键字。

Python程序打印所有关键字 (Python program to print all keyword)

# importing the module
import keyword

# getting the list of all keywords
List_of_key=keyword.kwlist

# printing the number of keywords
print("No of keyword in Python: ",len(List_of_key))

#To print lists of all keyword present in Python.
print("List of keyword:",List_of_key)

Output

输出量

No of keyword in Python:  33
List of keyword: ['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']

Note:

注意:

  • Here, we have seen that the total keyword in Python language is 33.

    在这里,我们已经看到Python语言中的total关键字为33。

  • Python does not allow us to use the keyword as a variable in the program.

    Python不允许我们在程序中使用关键字作为变量。

检查给定字符串是否是关键字的算法? (Algorithm to check given string is a keyword or not?)

  1. Initially, we will include the keyword module in Python by using the import function.

    最初,我们将通过使用import函数将关键字模块包含在Python中。

  2. Take the input string from the user.

    从用户那里获取输入字符串。

  3. Take a list of all keyword in a variable by using kwlist() function from the module keyword.

    通过使用来自module 关键字的 kwlist()函数 ,获取变量中所有关键字列表

  4. Check the given string is lies in the above-created list or not by using the keyword of Python.

    使用Python的关键字检查给定的字符串是否在上面创建的列表中。

    1. If it lies in the list then print the given string is a keyword in the Python programming language.
    2. If it does not lie in the list then print the given string is not a keyword in the Python programming language.

So, let's start writing the python program by the implementation of the above algorithm,

因此,让我们开始通过上述算法的实现来编写python程序,

检查给定字符串是否为关键字的Python程序 (Python program to check whether given string is a keyword or not)

# importing the module
import keyword

# input a string
str=input("Enter a string: ")

# getting a list of all keywords
List_of_key=keyword.kwlist

# checking whether given string is a keyword or not
if str in List_of_key:
    print("String {} is a keyword.".format(str))
else:
    print("String {} is not a keyword.".format(str))

Output

输出量

RUN 1:
Enter a string: includehelp
String includehelp is not a keyword.

RUN 2:
Enter a string: try
String try is a keyword.

RUN 3:
Enter a string: nonlocal
String nonlocal is a keyword.


翻译自: https://www.includehelp.com/python/check-whether-the-given-string-is-a-keyword-or-not.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值