Python function to check if a given string is a keyword

0 votes
I'm trying to write a python code to check if a given string is a keyword or not. Is there a function that lists down all keywords? or atleast something that lets me check if a string is a keyword or no. Thanks
Jun 17, 2019 in Python by Vaibhav

1 answer to this question.

0 votes

You can get the list of keywords in the current python version using kwlist method in keyword module. Import the keyword module and try something like this:

#import the keyword module
import keyword
#function to check if the word is a keyword
def isKeyword(word) :
#get keyword list
   keyword_list = keyword.kwlist
#check if they word is a prt of the keyword list
   if word in keyword_list :
      return "Yes"
   else :
      return "No"

answered Jun 17, 2019 by Drishya

Related Questions In Python

+1 vote
1 answer

How to check if a string is null in python

Try this: if cookie and not cookie.isspace(): # the ...READ MORE

answered Aug 20, 2018 in Python by Priyaj
• 57,700 points
0 votes
1 answer

How to check if a substring is present in a string using Python?

To check if the substring exists in ...READ MORE

answered May 9, 2019 in Python by Sharan
0 votes
1 answer

How to check if a given string matches a particular pattern in Python?

You can use re module of python ...READ MORE

answered Jul 3, 2019 in Python by Neel
• 3,010 points
+1 vote
1 answer

How to check if a string is in a file, using python?

Hi, Try the below given code: with open('myfile.txt') as ...READ MORE

answered Jul 4, 2019 in Python by Shabnam
• 930 points
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,480 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,800 points
0 votes
1 answer

How do I check if input string is a valid regular expression or not in Python?

Hi. Good question! Well, just like what ...READ MORE

answered Feb 12, 2019 in Python by Nymeria
• 3,520 points
0 votes
2 answers

What is the python logic to check if a number is armstrong number or not?

a = int (input('enter number')) num = a sum ...READ MORE

answered Jul 16 in Python by amrut
• 240 points