Python string function isidentifier ()

I am working on a Python 3 book and came across a string function called isidentifier (). Text description "s.isidentifier (): Returns True if s is not empty and is a valid identifier." I tested it in Python Shell as follows:

>>> s = 'test'
>>> s.isidentifier()
True
>>> 'blah'.isidentifier()
True

I would expect the second statement to return false, since "blah" is not stored in the variable. Can someone explain this? Thank.

+3
source share
4 answers

Returns True if s is not empty and is a valid identifier.

, , s . , , .

: "test" ( isidentifier ) . ,

>>> 's'.isidentifier()
True
+10

"isidentifier" "", .

'blah'.isidentifier()

s = 'blah'
s.isidentifier()

Python ( ) "" ( Python ), . .

+5

Python "". .

'blah' 'blah'.isidentifier() ( , " isidentifier() 'blah'" ).

, , isidentifier() True, .

isidentifier() , . Python, :

>>> a = "$"
>>> "$".isidentifier()

"$" a, isidentifier() False, $ Python.

+5

isidentifier - Python, , (, ) , Python. , , isalpha, isalnum, isdigit .

ss = (
    'varABC123',
    '123ABCvar',
    '_123ABCvar',
    'var_ABC_123',
    'var-ABC-123',
    'var.ABC.123',
    # check your own strings
)

fmt = '%-15s%-10s%-10s%-10s%-10s' 
print(fmt % ('', 'isalpha', 'isalnum', 'isdigit', 'isidentifier'))
for s in ss:
    print(fmt % (s, s.isalpha(), s.isalnum(), s.isdigit(), s.isidentifier()))

:

               isalpha   isalnum   isdigit   isidentifier
varABC123      False     True      False     True      
123ABCvar      False     True      False     False     
_123ABCvar     False     False     False     True      
var_ABC_123    False     False     False     True      
var-ABC-123    False     False     False     False     
var.ABC.123    False     False     False     False     
+2

Source: https://habr.com/ru/post/1727169/


All Articles