How To Print Unicode In Python 3

In this tutorial, we will learn how to print Unicode characters in Python 3. Unicode is an encoding standard that assigns a unique code point to each character for consistent representation and manipulation of text across various platforms and languages.

Python 3 supports Unicode natively, making it easier to handle text in different languages and scripts. Let’s explore the steps to print Unicode characters in Python 3.

Step 1: Getting the Unicode code point of a character

In Python 3, you can use the ord() function to obtain the Unicode code point of a given character. This function takes a single string argument, representing the character you want the Unicode code point for, and returns an integer value representing the code point. For example:

Output:

65

Step 2: Converting Unicode code points to characters

You can obtain the corresponding character for a Unicode code point using the chr() function in Python 3. The function takes an integer argument, representing the Unicode code point, and returns a single-character string. For example:

Output:

A

Step 3: Print Unicode characters

Now that we know how to obtain Unicode code points and convert them to characters, we can print Unicode characters in Python 3 programs. To do this, you can use the print() function and pass Unicode strings directly or print the characters obtained from the chr() function.

Unicode strings in Python 3 can be declared using the u prefix before the string. For example:

Output:

こんにちは

Alternatively, you can print the characters obtained from the chr() function as follows:

Output:

😀

Full Code

Here is the full code for printing Unicode characters in Python 3:

Output:

65
A
こんにちは
😀

Conclusion

In this tutorial, we have seen how to print Unicode characters in Python 3 using the ord() and chr() functions. We have also learned how to create Unicode strings and print them directly or print characters obtained from Unicode code points. This functionality makes it easy to work with text in various languages and scripts in Python 3.