How to determine if a string is a keyword in Python

1 Answer

0 votes
import keyword

s = "def"
print(keyword.iskeyword(s))

print(keyword.iskeyword('str')) # Python str() Function
 
print(keyword.iskeyword('for'))

print(keyword.iskeyword('startrek'))

 
 
 
 
 
'''
run:
 
True
False
True
False
 
'''

 



answered Feb 21, 2023 by avibootz

Related questions

...