What is the use of the .isidentifier () method in Python?

4

I came across the string .isidentifier() method and also the .isprintable() .

  • Why use them?
  • On what occasions would I use this?
asked by anonymous 13.08.2018 / 23:39

1 answer

4

These methods are parsers of a text, it sweeps across the text and analyzes whether all the characters without exception fit into certain rules, ie if all are within a range of characters that are valid for a particular use .

The isidentifier() will return True if all characters are valid for writing a handle in the code, then they are case-sensitive, as long as it is not the first character and a few more Unicode characters. A complete list of what is valid can be viewed at documentation .

The use of them is more when you are constructing some parser or some formulas generator, something that you will then need to use as an identifier, perhaps through a eval() (see more in Running arbitrary code in Python ? or What is the difference between these forms of command execution? ), that is, it is rare or even should not use.

The isprintable() will return True when all characters are visible when you print them, including the obvious ones, but also some less obvious ones such as blank space that are not printable. An example of an unprintable character is the null ( ). It has to take up space on screen or paper.

The most common thing to use is to know if it counts to determine the space occupied in any print on any medium or if it could cause some error because it creates an unexpected situation, for example a %code% which is a backspace , then it can rewind a character, so instead of occupying one more character it occupies less, deleting the previous character.

    
13.08.2018 / 23:58