Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I print a euro (€) symbol in Python?

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!)

  • I've tried prefixing the text string with u to create a unicode string in the first place.
  • I've tried creating an intermediate object outputtext = mytext.encode('utf-8') but outputting this with print expands the string into an even more cryptic form: b'Please pay \xc2\x8035.'
  • I've tried to find a different function instead of print to output this intermediate string, but nothing has worked yet.

Please can someone show me some code that just works, so I can study it and work backwards from there. Thanks!

like image 923
LD2k Avatar asked Oct 08 '16 18:10

LD2k


People also ask

How do I type the euro € symbol?

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.

How is € written?

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).

How do you use €?

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.


2 Answers

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 £
like image 111
Gopal Prasad Avatar answered Oct 03 '22 21:10

Gopal Prasad


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 €.'
like image 33
trincot Avatar answered Oct 03 '22 22:10

trincot