首页 > 解决方案 > 如何在 Python 3.x 中逐行打印关键字?

问题描述

在 Python 中,我们可以使用打印 35 个关键字print(keyword.kwlist)

结果将如下所示。

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', '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']

您能否告诉我,如何逐行打印关键字如下所示?

('Total number of keywords ',
 ['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'])

标签: pythonlistkeyword

解决方案


您可以使用列表解包,*然后将其分开'\n'

key_list=['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', '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']
print(*key_list,sep='\n')

推荐阅读