0


I want to print this character "¥" in python 2.7. Here is my code:

test = "¥"
print(test)

I execute this code and I get this error message:

SyntaxError: Non-ASCII character '\xc2' in file main.py on line 5, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

How to print this character? Thanks in advance

m0nhawk
  • 20,919
  • 9
  • 39
  • 68
Huy Phan
  • 5
  • 1
  • 4
  • Does this answer your question? [Correct way to define Python source code encoding](https://stackoverflow.com/questions/728891/correct-way-to-define-python-source-code-encoding) – The Zach Man Apr 24 '20 at 04:13
  • 1
    Does this answer your question? [SyntaxError: Non-ASCII character '\xa3' in file when function returns '£'](https://stackoverflow.com/questions/10589620/syntaxerror-non-ascii-character-xa3-in-file-when-function-returns-%c2%a3) – Joseph Sible-Reinstate Monica Apr 24 '20 at 04:13

1 Answers1

0

You need to specify the following line in the top of file:

# -*- coding: <encoding> -*-

Next time when you run this file Python will know what the encoding of that character is.

It will look like this, assuming you use utf-8.

# -*- coding: utf-8 -*-
test = "¥"
print(test)
m0nhawk
  • 20,919
  • 9
  • 39
  • 68