In a Python program that I am writing, I need to print the © (copyright) symbol. Is there an easy way to do this? Or is it not supported in Python? Here\'s an example.
print u"\u00A9"
where "\u00A9" is the unicode character of the copyright symbol.
In python 2:
>>> print u"\u00a9"
©
>>> print u"\N{COPYRIGHT SIGN}"
©
In python 3:
>>> print("\u00a9")
©
>>> print("\N{COPYRIGHT SIGN}")
©
>>> print("©")
©
In python 2 you must prefix the string with a u (u"..."
) to tell python its a unicode string. However, in python 3 all strings are unicode strings, so you don't have to (and actually aren't allowed to in 3.0-3.2) prefix the string with the u.
you can view a list of characters and their names / unicode values here: http://www.fileformat.info/info/charset/UTF-16/list.htm and use them the same way you are seeing the copyright symbol used here
In Python, you can put Unicode characters inside strings in three ways. (If you're using 2.x instead of 3.x, it's simpler to use a Unicode string—as in u"…"
instead of "…"
—and you have to use unichr
instead of chr
, but otherwise everything is the same.)
U
and 8 digits.'\N{COPYRIGHT SIGN}'
: Use a Unicode entity name escape sequence.
COPYRIGHT SIGN
is obviously more readable than 00a9
.You can also do things indirectly—e.g., unicodedata.lookup('COPYRIGHT SIGN')
or chr(0xa9)
will return the same string as the literals above. But there's really no reason not to use a literal.
The Unicode HOWTO in the Python docs has a lot more detail on this—if you're not willing to read the whole thing, The String Type describes the different kinds of escape sequences (and the issues with encoding/decoding between unicode and bytes strings, which are especially important in 2.x), and Unicode Literals in Python Source Code describes how to specify a coding declaration.
If you want an official list of all characters you can use, instead of just googling for them, look at the unicodedata docs for your version of Python, which contains links to the appropriate version of the Unicode Character Database. (For example, it's 6.1.0 in 3.3.0, 5.2.0 in 2.7.3, etc.) You'll have to navigate through a few links to get to the actual list, but this is the only way you'll get something that's guaranteed to be exactly what's compiled into Python. (And, if you don't care about that, you might as well just google it, or use Wikipedia or your computer's character viewer.)
Sure! Type the copyright symbol:
print("©")
(There aren’t character entities in Python like there are in, say, HTML.)
The copyright sign is a unicode character. If your terminal supports a character encoding (such as utf-8 or cp1252) that includes this character, then you can print it:
This relies on Python detecting the terminal's character encoding:
In [64]: print(u'\N{COPYRIGHT SIGN}')
©
This uses an explicit encoding (which happens to work since my terminal is set to use the utf-8 character encoding):
In [65]: print(u'\N{COPYRIGHT SIGN}'.encode('utf-8'))
©