How To Display Special Characters In Python With Print
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 than00a9
.
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:
>>>printu"\u00a9"
©
>>>printu"\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:
printu"\u00A9"
where "\u00A9" is the unicode character of the copyright symbol.
Post a Comment for "How To Display Special Characters In Python With Print"