In this tutorial, we’ll learn what Python string isidentifier() method is and how to properly use it. We’ll go through multiple Python code examples to understand how isidentifier() method works.
Outline
- Introduction: Python String isidentifier() Method
- Syntax of isidentifier() Method
- Example 1: isidentifier() Method applied on a String
- Example 2: isidentifier() used in If Else Condition inside Python For Loop
- Example 3: isidentifier() Method Return Value
- Conclusion
Introduction: Python String isidentifier() Method
This method will return True if a specified string is a valid Python identifier, or else it will return False.
Syntax of isidentifier() Method
string.isidentifier()
- Python string isidentifier() method does not take any arguments.
- This method returns True if the string is a valid Python identifier and will return False if its not.
Example 1: isidentifier() Method applied on a String
strVal='Pyhton' print(strVal.isidentifier()) print('_Python'.isidentifier())
Output
True True
The specified string is a valid identifier so Python string isidentifier method return True.
Below examples include some non-valid identifiers.
print('2Python'.isidentifier()) print('Pyth on'.isidentifier()) print('&Python'.isidentifier()) print('$Python'.isidentifier()) print('2.5Python'.isidentifier()) print('python2@&$'.isidentifier())
Output
False False False False False False
Example 2: isidentifier() used in If Else Condition inside Python For Loop
listStr=['Programming','2Python','Pyth on','_Python','&Python','$Python','python$'] for i in listStr: if i.isidentifier()==True: print(i+' is a valid identifier') else: print(i+' is not a valid identifier')
Output
Programming is a valid identifier 2Python is not a valid identifier Pyth on is not a valid identifier _Python is a valid identifier &Python is not a valid identifier $Python is not a valid identifier python$ is not a valid identifier
Example 3: isidentifier() Method Return Value
val='PythonProgram3' returnedVal=val.isidentifier() print(returnedVal)
Output
True
Python string isidentifier method return True as the string is a valid identifier.
Conclusion
To conclude this tutorial, hope you now have an in-depth practical knowledge of how to properly use Python string isidentifier method. I’ll be looking forward to receive your valuable feedback on this post. Thank you for reading it.
You may like to read:
How To Use Python String IsDigit() Method – Easy Python Code Guide
How To Use Python String IsAlpha() Method – Easy Python Code Guide
How To Use Python String IsDecimal() Method – Easy Python Code Guide
How To Use Python String Find() Method – Easy Python Code Guide