Reputation: 199
The following code returns “Error: Invalid Syntax” when reaching the third line:
# -*- coding: utf-8 -*-
print “åäö”
ÅÄÖ = 4
The print statement works fine despite holding non-ASCII characters. The problem seems to be when the non-ASCII characters are used as variable names. Doing this in Python 3 works perfectly fine, and I’ve understood that this is because Python 3 and 2 treat strings differently, but I need to use Python 2 for various reasons.
Being able to write the letters åäö in Python 2.7 would be extremely valuable to me, partly because I am fairly new to programming in Python and strongly prefer naming my variables and functions in Swedish as it makes them much easier to separate from Pythons in-built functions.
Upvotes: 4
Views: 2329
Reputation: 2847
You can't do this. The language syntax specification simply doesn't allow it for Python 2.x:
https://docs.python.org/2/reference/lexical_analysis.html#identifiers
As you already discovered this was changed for Python 3 where the syntax allows certain kinds of non-ascii characters to occur in identifiers
https://docs.python.org/3/reference/lexical_analysis.html#identifiers
(Note that identifiers are something else entirely as string literals!)
Upvotes: 7