Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

printing - How to display special characters in Python with print

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...