Search code examples
pythoncharacter

What is $ character to Python?


I just realized I had never seen a dollar $ character in Python, other than in a string. Does it have any defined usage, or is it reserved for future use? Google doesn't provide an answer. Experimentally it causes syntax errors. Just about any other "visible" unicode (not sure of correct unicode terminology) is accepted as part of a variable name (Python 3)

>>> aμ = 44
>>> aμ
44

>>> a$=27
  File "<stdin>", line 1
    a$=27
     ^
 SyntaxError: invalid syntax

Solution

  • Per the lexical analysis documentation:

    The following printing ASCII characters are not used in Python. Their occurrence outside string literals and comments is an unconditional error:

    $       ?       `
    

    The dollar sign, along with question mark and backtick, is simply unused in Python.

    For variable names (identifiers) specifically (emphasis mine):

    Within the ASCII range (U+0001..U+007F), the valid characters for identifiers are the same as in Python 2.x: the uppercase and lowercase letters A through Z, the underscore _ and, except for the first character, the digits 0 through 9.

    That is, although Python 3 added support for additional Unicode characters outside the ASCII range in identifiers, other characters inside that range (including $) are still not supported.