-1

In Python, we can print 35 keywords using print(keyword.kwlist)

The result will be as below.

['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']

Could you please let me know, How to print Keywords Line by Line as shown below?

('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'])
wjandrea
  • 28,235
  • 9
  • 60
  • 81
KSNR1947
  • 21
  • 2
  • 1
    what have you tried so far? – Ryan Schaefer Aug 06 '21 at 16:27
  • 2
    That is for visual purposes. You can do ```print(*['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'],sep='\n')``` –  Aug 06 '21 at 16:28
  • @Sujay Please don't post answers in the comments. Post an answer instead please. – wjandrea Aug 06 '21 at 16:30
  • I rolled back your edit. [Please don't post pictures of text](https://meta.stackoverflow.com/q/285551/4518341) – wjandrea Aug 06 '21 at 16:40

2 Answers2

1

You can use list unpacking with * and then separate it with '\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')
1

You can use pprint in the standard library

import pprint

keywords = [
    "and",
    "as",
    # etc
]

pprint.pprint(keywords, width=-1)
Jacob
  • 750
  • 5
  • 19