Python String isspace ()

De methode isspace () retourneert True als er alleen spaties in de tekenreeks staan. Als dit niet het geval is, wordt False geretourneerd.

Tekens die worden gebruikt voor spaties, worden spaties genoemd. Bijvoorbeeld: tabbladen, spaties, nieuwe regel, etc.

De syntaxis van isspace()is:

 string.isspace ()

isspace () Parameters

isspace() methode heeft geen parameters.

Retourwaarde van isspace ()

isspace() methode retourneert:

  • True als alle tekens in de string witruimtetekens zijn
  • False als de tekenreeks leeg is of ten minste één niet-afdrukbaar teken bevat

Voorbeeld 1: werking van isspace ()

 s = ' ' print(s.isspace()) s = ' a ' print(s.isspace()) s = '' print(s.isspace())

Uitvoer

 Juist Fout Fout

Voorbeeld 2: Hoe isspace () te gebruiken?

 s = ' ' if s.isspace() == True: print('All whitespace characters') else: print('Contains non-whitespace characters') s = '2+2 = 4' if s.isspace() == True: print('All whitespace characters') else: print('Contains non-whitespace characters.')

Uitvoer

 Alle witruimtetekens Bevat niet-witruimtetekens

Interessante artikelen...