2

Just a sample question. Is it possible to show the symbol ± in Python? I just tried to use the Extended ASCII Codes char(241), but it didn't work.

The simple code:

b = chr(241)    #The Extended ASCII Codes for 241 is this symbol "±".
print(b)

The result showed UnicodeEncodeError: 'ascii' codec can't encode character '\x80' in position 0: ordinal not in range(128)

Can someone help? Thanks

I just want to test if I could show the Extended ASCII Codes in Python. There is no complicated code.

Here is the code and result: enter image description here

Neil A.
  • 841
  • 7
  • 23
S. Li
  • 767
  • 1
  • 7
  • 14
  • @abccd Got it. I just thought, there are actually only one assignment and one print statement in the code. It's not very necessary to write the code. Th e screenshot is more clear. So could you please tell me how to show this symbol correctly? Thanks. – S. Li Jun 10 '17 at 06:52
  • 2
    What's wrong with `print('±')`? – DYZ Jun 10 '17 at 06:55
  • @DYZ It cannot be showed. I said the result in the question. – S. Li Jun 10 '17 at 06:58
  • 2
    Possible duplicate of ["UnicodeEncodeError: 'ascii' codec can't encode character"](https://stackoverflow.com/questions/1652904/unicodeencodeerror-ascii-codec-cant-encode-character) – DYZ Jun 10 '17 at 07:00
  • Try `b = '\x00\xB1'` (python 3) – cdarke Jun 10 '17 at 07:02
  • @cdarke I tried. Same result. It said that "'ascii' code can't encode character '\xb1' in position 1: ordinal not in range(128)". – S. Li Jun 10 '17 at 07:05
  • range(128) is basic 7-bit ASCII, not extended. Which version of python are you using, and are you running it on a console which supports extended ASCII or Unicode? For example, Windows cmd.exe has poor support for these characters (although I have not tried that one). – cdarke Jun 10 '17 at 07:11
  • @cdarke I use 3.6.1. And I run the code in Sublime Text. – S. Li Jun 10 '17 at 07:13
  • 1
    I'm also running 3.6.1 and it works fine for me. Check the Sublime documentation for console settings, or run your program from a command-line which supports the correct character set. – cdarke Jun 10 '17 at 07:18
  • 1
    @cdarke Got it. It works with the command-line. – S. Li Jun 10 '17 at 07:21
  • Check https://stackoverflow.com/questions/37121558/sublime-text-console-not-showing-lines-with-accents – cdarke Jun 10 '17 at 07:23

1 Answers1

1

Summarizing the comments, any of the following alternatives can be applied

print('±') # Provided your terminal supports Unicode
print('\xB1') # For all cases.
Ébe Isaac
  • 11,563
  • 17
  • 64
  • 97