What characters can you use in Python variable names?

The exact definition of what characters are valid can be found in the Python docs, but a broad definition could be "all characters that can be used in written languages".

Here are some examples of Python variables that use non-ASCII characters:

빵 = "bread"  # Korean
ψωμί = "bread"  # Greek
面包 = "bread"  # Chinese (Simplified)
ขนมปัง = "bread"  # Thai
pão = "bread"  # Portuguese
талх = "bread"  # Mongolian
パン = "bread"  # Japanese

(I used Google translate for this... Please let me know if there is a mistake here!)

The exact list of all valid characters is gigantic. But, in the midst of that gigantic list, four characters stand out:

These four characters are invisible but I do not think they are considered whitespace. At least, not to the Unicode standard. And, thus, they can be used in Python variables!

First, let me show you the literal transcript of a REPL session in which I use those four characters to represent four variables:

>>> def ᅟ(a, b):   # Function name is U+115F
...     return a + b + 1
... 
>>> ᅠ = 6          # Variable name is U+1160
>>> ㅤ = 8         # Variable name is U+3164
>>> ᅠ = ᅟ(ᅠ, ㅤ)  # Result is U+FFA0
>>> ᅠ
17

Depending on your system, you may be able to see something for the first two characters, but the last two should be invisible! So, you can have invisible variable names in Python.

That's it for now! Stay tuned and I'll see you around!

Improve your Python 🐍 fluency and algorithm knowledge 🎯

Get ready for 12 intense days of problem-solving. The “Algorithm Mastery Bootcamp” starts December 1st and it will feature 24 programming challenges, live analysis sessions, a supportive community of like-minded problem-solvers, and more! Join now and become the Python expert others can rely on.

References

Previous Post Next Post

Blog Comments powered by Disqus.