-3

Initially I installed version 3.4 of Python and then I added version 2.7 as a video course instructor was using it as well.

After I opened IDLE and wrote some code, pressed F5 a meessage popped up. I accepted the message and from then on this line was added in IDLE on the top of my file:

# -*- coding: utf-8 -*-

I tried to read it through wikipedia but it is too long. Could you be able to explain me if that is a consequence of having two versions of the python installed in my computer and in layperson words; what is utf-8?

Burhan Khalid
  • 152,028
  • 17
  • 215
  • 255
  • Have a look at [What is unicode](http://stackoverflow.com/a/15128103/1401034). On your multiple pythons installed it means exactly that, you can run your program with either 2.7 or 3.4. There are [syntax differences](https://wiki.python.org/moin/Python2orPython3) between the two versions – Ewan Dec 17 '14 at 12:19
  • Stop with the down vote floods please as this is a legitimate (albeit badly worded) question. – Burhan Khalid Dec 17 '14 at 12:26
  • I assume that you were running 2.7 and that you typed some non-ascii characters. The same would have happened without installing 3.4 first. For 2.7, if you look at Options - > Idle Preverences -> General (tab), you will see your choices for Default Source Encoding. If utf-8 is checked because of your choice in response to the prompt, I would leave it unless you really know that your should change it. – Terry Jan Reedy Dec 17 '14 at 21:06
  • @TerryJanReedy yes. UTF-8 is checked. I will leave. Honestly, right now I do not know why and when I should change that option. This is my 5th day into programming in python and I still need to learn a lot of material. – Aleksandar Gavrič Dec 17 '14 at 21:25
  • The only reasons I can think of would be if an instructor required you to submit programs with a different encoding or if you wanted to real your .py files with software that absolutely refused to read utf-8. I hope neither of these are ever true. – Terry Jan Reedy Dec 17 '14 at 21:32
  • Neither of these are true. Initially I started to learn to code with 3.4. Yesterday I started with MOOC on python programming for beginners. The instructor is using 2.7. so I wanted to code in 2.7. as well and this is why I installed this version as well. However, after I started I experienced this utf-8 event. After the conversation with u guys I have more clear picture about what utf-8 and encoding is, but for non native english and non tech starting to learn to code is not a piece of cake :) – Aleksandar Gavrič Dec 17 '14 at 21:38

1 Answers1

1

In Python, when you write some code and create some strings, for example:

x = 'Hello'
print(x)

Python needs to know what encoding to use to read the strings. Its not a problem if you are only using English (as this is the default), but if you write something like this:

x = 'Aleksandar Gavrič'
print 'My name is %s' % x

Python does not know what č is (since for some very old reasons it only understands English by default). To tell it how to read č correctly, you need to tell it what encoding the file is using.

IDLE is smart enough to detect when you are typing non-English characters in strings and will prompt and suggest you to add a special comment to the top of the file to help Python read your code - that's what you clicked on and the result was that line was added to the top.

For more details, you can read the Python documentation which describes what that line is, why its needed etc.

For more on what is encoding and utf-8, take a few minutes and have a read through this excellent article which explains this concept and history behind it.

Burhan Khalid
  • 152,028
  • 17
  • 215
  • 255