enter image description here enter image description here Good day. I try to write in the command print ('any text with Russian letters') and actually nothing happens. I write the text in English, everything works. Can you tell me how to fix it?

Language - Python

Reported as a duplicate by KoVadim members, user194374, aleksandr barakin , zRrr , cheops Jul 1 '16 at 18:14 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • We have to guess what programming language is used? - user194374
  • and what language? python? - KoVadim
  • I apologize for the omission: D Yes, Python - Artem
  • make sure that Visual Studio saves the original file ( .py ) using utf-8 encoding, and not cp1251 or cp866. - jfs

1 answer 1

If you have python2, then you have all the default strings - byte. Consequently, when you try to output you get an error.

In order to convert a byte string to unicode, you must either add an explicit type conversion (eg print (unicode (my_string))), or, if the string is specified directly in the file or interpreter, tell python that it is a unicode string by adding the character 'u 'before opening quotation mark, for example, print (u' string ')

To solve this problem in recent versions of python 2 (in particular in 2.7), Unicode strings from python 3 are ported. In order for all the lines in the .py file to become Unicode, you need to add at the top of the file

 from __future__ import unicode_literals 

If you are trying to do this in a .py file, then you must specify the encoding of this file as the first line, as python2 considers this ascii file. This can be done by adding the line

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

at the beginning of the file.

In python 3, all unicode strings and such problems usually do not occur.

Updated 2

Correct the file so that it looks like this.

 # -*- coding: utf-8 -*- print('ку-ку') 

Updated 3

coding: utf-8 only tells python that the contents of the file should be interpreted as utf-8. Check in your editor if this is true. It seems to me if the solution above does not help, then the problem is precisely this - your editor saves in windows-1251 encoding. We need to find where it is configured and changed.

  • Comments are not intended for extended discussion; conversation moved to chat . - Nicolas Chabanovsky
  • Try STDOUT_CODING = sys.stdout.encoding. print (text.encode (STDOUT_CODING, 'replace'). decode (STDOUT_CODING) - Chp