How To Check If a String is a Valid Keyword In Python - Programmer's Academy

How To Check If a String is a Valid Keyword In Python

Introduction

Recalling strings: String is a data type in python which is used to store words or sentences in a variable, for example. a = “I am a python programmer”. In this example I am a programmer I s a sentence stored in a string “ a “ .  Now we can assign sentence to any variable to store string such as a, b, c ,num etc. But there are some words which are prohibited to be used to store any data in them. What are they? Let us understand keywords first.

Keywords

Keywords are reserved words that are assigned a special task or function by the compiler. They are necessary part of a programming language. Keywords cannot be used a variable to store any data type either int, string or others. The reason is that keywords have already defined functionality inside the compiler, and they have their task assigned already so cannot be over looped. For is a reserved keyword to use for loop. Let us see on the compiler

It did not display value for “I am a python programmer” because it was stored in a reserved keyword. According to askpython.com there are 35 keywords in python.

Check if string is valid

Now we will see through an example that if you are using some variable as a string either it is reserved keyword or it is valid to be used.

Step 1:

Import keyword library in python compiler, it will be used to distinguish keywords from variables we will assign. This can be done using.

import keyword

Step2:

Create a function using def checkFunction. Pass string inside its parameter for which you want to check is valid or not.

Def checkFunction(mystring)

Step 3:

Compare list of keywords with the one you are trying to check using.

keyword_list = keyword.kwlist

if mystring in keyword_list :

return “It is a reserved keyword”

else :

return “It is a valid string

Step4:

Simply add you desired string to be checked inside parameter for this function.

print(checkFunction(“Your string that you want to check “))

Step5: Final code

  1. import keyword
  2. def checkFunction(mystring) :
  3. keyword_list = keyword.kwlist
  4. if mystring in keyword_list :
  5. return “It is a reserved keyword”
  6. else :
  7. return “It is a valid string”
  8. print(checkFunction(“mango”))
  9. print(checkFunction(“while”))
Output
Total Keywords in python

False                break               for                     not

None               class               from                    or

True                continue            global              pass

__peg_parser__      def               if                  raise

and                 del                    import                return

as                  elif                      in                             try

assert              else                     is                         while

async               except              lambda                with

await               finally             nonlocal                yield

References

Help Me Please: If you found any error in the content or example please let us know by comment also write your suggestion or if any doubt in the concept please feel free to write us in the comment section.

Practice your Code online here

(Visited 39 times, 1 visits today)

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
You cannot copy content of this page
Scroll Up
0
Would love your thoughts, please comment.x
()
x

  • Name

  • Minimum length of 8 characters.