What is the . isidentifier() method used in Python?

Asked

Viewed 1,426 times

5

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

  • Why use them?
  • On what occasions would I wear this?
  • Now, in addition to accepting answers to your questions, you can vote all over the site as well, any answers or questions that please you.

1 answer

5


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

The isidentifier() will return True if all characters are valid to write an identifier in the code, then they are uppercase and lowercase letters, digits, as long as it is not the first character plus some Unicode characters. The full list of what is valid can be seen on documentation.

Their use is over when you’re building some parser or some formula generator, something you’ll need to use as an identifier, perhaps through a eval() (see more in How to execute arbitrary code in Python? or What is the difference between these forms of command execution?), that is, is rare or even should not use.

The isprintable() will return True when all the characters are visible when you have them printed, this includes the obvious ones, but also some less obvious ones like the ones that generate blank space that are still printable. An example of a non-printable character is null (\0). It has to occupy space on screen or paper.

The most common use of it is to know that if it will count to determine the space occupied in a print any support or if it may cause some error why create an unexpected situation, for example a \8 which is a Backspace, then he can rewind a character, so instead of occupying one more character he occupies less, erasing the previous one.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.