Python String isidentifier ()

Il metodo isidentifier () restituisce True se la stringa è un identificatore valido in Python. In caso contrario, restituisce False.

La sintassi di isidentifier()è:

 string.isidentifier ()

isidentifier () Parametri

Il isidentifier()metodo non accetta parametri.

Valore restituito da isidentifier ()

Il isidentifier()metodo restituisce:

  • Vero se la stringa è un identificatore valido
  • Falso se la stringa non è un identificatore non valido

Esempio 1: come funziona isidentifier ()?

 str = 'Python' print(str.isidentifier()) str = 'Py thon' print(str.isidentifier()) str = '22Python' print(str.isidentifier()) str = '' print(str.isidentifier())

Produzione

 Vero Falso Falso Falso

Visita questa pagina per sapere cos'è un identificatore valido in Python?

Esempio 2: altro esempio di isidentifier ()

 str = 'root33' if str.isidentifier() == True: print(str, 'is a valid identifier.') else: print(str, 'is not a valid identifier.') str = '33root' if str.isidentifier() == True: print(str, 'is a valid identifier.') else: print(str, 'is not a valid identifier.') str = 'root 33' if str.isidentifier() == True: print(str, 'is a valid identifier.') else: print(str, 'is not a valid identifier.')

Produzione

root33 è un identificatore valido. 33root non è un identificatore valido. root 33 non è un identificatore valido.

Articoli interessanti...