Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 249
Evan Avatar asked Feb 27 '13 00:02

Evan


People also ask

How do you represent special characters in Python?

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return. Conversely, prefixing a special character with "\" turns it into an ordinary character.

How do you print special characters?

Press Ctrl + Shift + u. Type in hexadecimal code for Unicode character you want to print. Press enter.

How do I print special characters in a string?

Use the repr() function to print a string with the special characters, e.g. print(repr(my_str)) . The repr() function returns a string containing a printable representation of the provided object. Copied! We used the repr() function to print a string with the special characters.

How do you print a reserved character in Python?

, \t, \r, etc., What if we want to print a string which contains these escape characters? We have to print the string using repr() inbuilt function. It prints the string precisely what we give.


1 Answers

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'))
©
like image 51
unutbu Avatar answered Sep 29 '22 01:09

unutbu