2

I have been trying to figure out how to use non-ascii unicode characters as Python 3 variables but I'm not sure which ones work and which ones don't. Why does the σ work but does not? Is it possible to use as a character or is it impossible at the moment?

# Version
sys.version
'3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 12:04:33) \n[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]'

σ = 10
print(σ )
# 10

∆ = 20
#   File "<ipython-input-24-b5e030117292>", line 1
#     ∆ = 20
#     ^
# SyntaxError: invalid character in identifier
1
  • The documentation for Python3 identifiers is here. Commented Mar 14, 2018 at 17:53

1 Answer 1

7

This is documented under Identifiers in the Python reference.

But the short version is that you can only use letters and numbers and a few special connectors in identifiers, and (U+2206, INCREMENT) is not a letter, it's a mathematical operator (part of the Symbol, Math category).

If you were intending to use Δ (U+0396, GREEK CAPITAL LETTER DELTA)… well, that's an understandable mistake, since they not only look very close or identical in most fonts, but are usually not distinguished even in blackboard writing (the whole point of the "increment" symbol is that it's a delta). But they're not the same character, and only the one that's meant to be used as a letter can be part of an variable name.

Also, the docs mention but don't link to the Unicode standard that Python's standard is based on, so here's the link: UAX-31: Unicode Identifier and Pattern Syntax (aka TR-31).

Sign up to request clarification or add additional context in comments.

1 Comment

Never would I have guessed that Δ is different than ∆! Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.