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
2,169 views

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"

print(isKeyword("Machine"))
print(isKeyword("while"))
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
• 58,020 points
23,098 views
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
1,144 views
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,020 points
1,202 views
+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 5, 2019 in Python by Shabnam
• 930 points
2,032 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,441 views
0 votes
1 answer
+5 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,790 points
4,077 views
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,560 points
11,292 views
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, 2020 in Python by amrut
• 240 points
1,776 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP