How to display special characters in Python with print

67,531

Solution 1

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

  • '©': Type it directly.
    • This means you will probably have to pick a character encoding for your source code—e.g., explicitly save the file as UTF-8, and put an encoding header at the top of it. (For 3.3, UTF-8 is the default, so you don't need the encoding header if that's what you use.)
    • Under Mac OS X, in most languages' default keyboard setup, this is opt-G.
    • Under Windows, I believe you can use the alt-numeric-keypad trick with 0169 to enter it, although that doesn't seem very easy.
    • If you don't know how to type '©' with your keyboard, copy and paste it from elsewhere (google "copyright sign" and you should find a page you can copy it from—or, for that matter, right from here).
    • Or, your computer probably has a Character Viewer or something similar that lets you point and click at special characters.
  • '\u00a9': Use the Unicode numeric escape sequence.
    • Google for "unicode copyright sign", and you'll quickly see that it's U+00A9. In Python, that's `'\u00a9'.
    • For anything outside the Basic Multilingual Plane—that is, more than 4 hex digits, use a capital U and 8 digits.
  • '\N{COPYRIGHT SIGN}': Use a Unicode entity name escape sequence.
    • Again, you'll probably need to google to find the right name for the entity.
    • It isn't entirely documented what names you can and can't use. But it generally works when you expect it to, and 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.)

Solution 2

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

Solution 3

Sure! Type the copyright symbol:

print("©")

(There aren’t character entities in Python like there are in, say, HTML.)

Solution 4

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'))
©

Solution 5

print u"\u00A9"

where "\u00A9" is the unicode character of the copyright symbol.

Share:
67,531
Evan
Author by

Evan

Updated on September 11, 2020

Comments

  • Evan
    Evan almost 4 years

    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 ("\(copyright symbol here\)") 
    

    Just a very simple problem. Thanks!

  • Sinkingpoint
    Sinkingpoint over 11 years
    I'll point out that depending on your location, the copyright symbol is not exactly commonplace on keyboards...
  • Ry-
    Ry- over 11 years
    @Quirliom: Is it anywhere? I typed it using Compose, O, C, but otherwise, Character Maps could be the way to go.
  • El Hocko
    El Hocko over 11 years
    you could type this: print("\xa9") (in python3.2)
  • El Hocko
    El Hocko over 11 years
    or this: print(chr(169)) but note that this is still utf-8 encoding
  • abarnert
    abarnert over 11 years
    @cIph3r: No, in Python 3, chr(169) (or, probably better, chr(0xa9) is not UTF-8, it's Unicode character #169. (And I assume you're using 3.x, given your previous comment and the fact that you're using print as a function, you're referring to 3.x.)
  • abarnert
    abarnert over 11 years
    Actually, you are allowed to prefix strings with u in Python 3.2 and later. (Of course it has no effect—but it's very handy for writing code that works with both 2.6-2.7 and 3.2+.)
  • Ryan Haining
    Ryan Haining over 11 years
    it was put back in for 3.3, but yes I should note that, thanks
  • El Hocko
    El Hocko over 11 years
    @abarnert 1. in fact, UTF (like utf-8) is an implementation of the unicode character-set 2. 169 is decimal for 0xa9, why is hex "probably better"?! 3. As you can see, I am referring to python3.x since I wrote python3.2, which is a version of python3.x (x=2)
  • abarnert
    abarnert over 11 years
    @cIph3r: 1. But chr(169) does not return a UTF-8 string. It returns a Unicode string, whose underlying encoding is either explicitly UTF-16 or -32 (3.0-3.2), or an unspecified implementation detail (3.3), not UTF-8. 2. Hex is probably better because any reference, help page, etc. that gives you the number for the © character will give it to you in hex, e.g., as U+00A9. So you (and your readers) will not have to convert back and forth. 3. Did you not read the parenthesized comment, where I explicitly said I assume you're using 3.x because you said 3.2 in the previous comment?
  • abarnert
    abarnert about 9 years
    @LastTigerEyes: Don't post new questions as comments on existing answers. If you have a new question, create a new question. You can link back to this question or answer by using share to get a URL that you can paste into the new question. But please search for this one first; the problem printing Unicode output to the Windows cmd.exe terminal has been asked and answered dozens of times on this site, if not hundreds.