Why Python 3 Embraces Unicode Variable Names—and What Old Books Get Wrong
The article reveals that many recent Python books mistakenly claim variable names are limited to letters, digits, and underscores, explains how Python 3 fully supports Unicode identifiers—including Chinese characters—citing official documentation, PEP 3131, and practical code examples.
While reviewing two recent Python books, I discovered they both repeat a serious mistake: they state that variable names are limited to letters, digits, and underscores, a rule that belongs to Python 2.
In fact, Python 3.x fully supports Unicode identifiers, allowing even Chinese characters as variable names. For example:
>>> 姓名 = "Python猫"
>>> print(f"我是{姓名},欢迎关注!")
我是Python猫,欢迎关注!Many newer books, especially translations, still propagate the outdated rule, which can mislead beginners.
Identifiers (names) are used to label variables, constants, functions, classes, and other entities. When defining an identifier, three basic questions arise:
Which characters may it contain?
Is it case‑sensitive?
Are any words reserved as keywords?
Historically, most languages (in early versions) required identifiers to consist only of letters, digits, and underscores, and they could not start with a digit. Some languages allowed special symbols like $, @, or % (e.g., PHP, Ruby, Perl).
Python’s early versions (pre‑3.0) followed this rule. The official Python 2 documentation described identifiers as:
identifier ::= (letter|"_") (letter | digit | "_")*
letter ::= lowercase | uppercase
lowercase ::= "a"..."z"
uppercase ::= "A"..."Z"
digit ::= "0"..."9"Since Python 3.0, this restriction was lifted. The current Python 3 documentation shows that identifiers may contain any Unicode character, as illustrated in the official spec.
Unicode, introduced in 1994, has been gradually adopted by many programming languages; today at least 73 languages support Unicode variable names.
Python officially added support for non‑ASCII identifiers in PEP 3131 – Supporting Non‑ASCII Identifiers , published during the development of Python 3.0.
Consequently, variable names can be written in many scripts. Some examples (use with caution):
>>> ψ = 1
>>> Δ = 1
>>> ಠ_ಠ = "hello"In summary, some Python books still contain outdated information about variable naming rules, which should be corrected. Python 3, as a modern and internationalized language, fully supports Unicode identifiers, though whether to use Chinese names in projects is a separate consideration.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
