I'm teaching myself Python using the command-line interpreter (v3.5 for Windows).
All I want to do is output some text that includes the euro (€) symbol which I understand to be code 80h (128 dec).
#!
# -*- coding: utf-8 -*-
mytext = 'Please pay \x8035.'
print(mytext)
It falls over on the last line:
UnicodeEncodeError: 'charmap' codec can't encode character '\x80' in position 11: character maps to <undefined>
I've done lots of googling (re encodings etc) and I've a rough idea why the print command fails. Tinkering with the above code shows that ASCII codes up to \x7f work fine, as one might expect.
But I can't figure out how to display the €, and I'm finding the information on encodings overwhelming and impenetrable. (Remember I'm just a noob!)
Please can someone show me some code that just works, so I can study it and work backwards from there. Thanks!
Inserting the euro symbol using an Alt keyboard shortcut Position the cursor where you want to insert the euro symbol. Press and hold Alt + 0128 on the numeric keypad.
If you are writing out an amount in euros, use the currency symbol or euro sign € . Note that the symbol € goes before the amount and that there is no space between them (e.g. € 50).
In Statistics Explained articles the symbol '€' should be used for euro in the text if it is followed by a number. This applies also to graphs and tables. It should be placed before the figure: €30.
You can use Unicode Character Name
\N{name}
is great way to print unicode character symbol.
Here is sample run....
>>> print "Please pay %s"%(u"\N{euro sign}")
Please pay €
>>> print "Please pay %s"%(u"\N{dollar sign}")
Please pay $
>>> print "Please pay %s"%(u"\N{rupee sign}")
Please pay ₨
>>> print "Please pay %s"%(u"\N{pound sign}")
Please pay £
You can use this:
mytext = 'Please pay \u20ac.'
print(mytext)
... based on Unicode Character 'EURO SIGN'.
But if the character can be represented in the script's character encoding, then there is no reason why you shouldn't write:
mytext = 'Please pay €.'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With