[Tutor] python3 - °F/°C printing those degree signs
Steven D'Aprano
steve at pearwood.info
Tue Aug 7 19:41:46 EDT 2018
On Tue, Aug 07, 2018 at 02:32:58PM -0700, Evuraan wrote:
> Greetings! How to print °F/°C etc in python3?
In Python 3, you should be able to do:
print('°F/°C')
directly. If you can't, your configuration is broken.
If you are including this is a .py file, make sure your text editor is
set to use UTF-8 as the encoding.
> (This works on a WSL):
WSL?
> ~$ python3
> Python 3.5.2 (default, Nov 17 2016, 17:05:23)
> [GCC 5.4.0 20160609] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import platform
> >>> platform.release()
> '4.4.0-17134-Microsoft'
Microsoft Linux?
> >>> print('\u00b0'+ " F")
> ° F
You don't need to use escape codes for this, but if you do, try this:
print('\u00b0 F')
> Elsewhere, it no longer seem to work:
>
> $ python3
> Python 3.5.2 (default, Nov 23 2017, 16:37:01)
> [GCC 5.4.0 20160609] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import platform
> >>> platform.release()
> '4.4.0-21-generic'
What is this? OS X (Macinintosh), Windows, Windows with cgwin, Linux,
some other Unix?
What does os.name return?
> >>> print('\u00b0'+ " F")
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> UnicodeEncodeError: 'ascii' codec can't encode character '\xb0' in
> position 0: ordinal not in range(128)
Until now, I would have said that error is literally impossible in
Python 3.5.
Unless you have made a copy-and-paste error, and aren't showing us the
correct output, I can't imagine how you are getting that error. This is
very weird.
Hmmm... thinking... what do these return?
sys.getdefaultencoding()
sys.stdout.encoding
--
Steve
More information about the Tutor
mailing list