Remember
isidentifier() checks whether text has the lexical form of a Python identifier. A reserved keyword can still pass this check.
isidentifier() checks whether text has the lexical form of a Python identifier. A reserved keyword can still pass this check.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = 'user_name'
result = text.isidentifier()
print(result)Top-level scope
'user_name'Object 2 · boolTrueisidentifier() checks whether text has the lexical form of a Python identifier. A reserved keyword can still pass this check.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
text = 'user_name'
result = text.isidentifier()
print(result)
|
v
DISPLAYED RESULT
True
FINAL BINDINGS
text --> object 1 (str)
result --> object 2 (bool)
object 1: 'user_name'
object 2: TrueChoose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = 'user_name'
result = text.isidentifier()
print(result)Top-level scope
'user_name'Object 2 · boolTrueThe example has finished. Compare the final output with the values and references above.
True
What do you expect this program to print?
text = 'user_name'
result = text.isidentifier()
print(result)String isidentifier() MethodThe isidentifier() method in Python is used to check whether a given string is a valid Python identifier. An identifier is a name used to identify variables, functions, classes, modules, etc.
string.isidentifier()
True if the string is a valid identifier.False otherwise.isidentifier() won't check for keywords)name = "my_variable"
print(name.isidentifier())
True
name = "1variable"
print(name.isidentifier())
False
name = "my var"
print(name.isidentifier())
False
name = "_temp123"
print(name.isidentifier())
True
keywords – it doesn't. "class" is a valid identifier technically, but cannot be used as a variable.isalnum() – which only checks for letters and digits, not identifier rules.Be ready to explain the difference between isidentifier() and isalnum(), and what makes a valid identifier in Python.
isidentifier() checks if a string is a valid Python identifierTrue if valid, False if notWrite a program that takes input from the user and tells whether it's a valid identifier.
user_input = input("Enter a variable name: ")
if user_input.isidentifier():
print("Valid identifier")
else:
print("Invalid identifier")
Connect what you noticed in the example with the idea below.
isidentifier() checks whether text has the lexical form of a Python identifier. A reserved keyword can still pass this check.
isidentifier() alone guarantees that a name is not a reserved keyword.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.