Open In App

How To Print Unicode Character In Python?

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Unicode characters play a crucial role in handling diverse text and symbols in Python programming. This article will guide you through the process of printing Unicode characters in Python, showcasing five simple and effective methods to enhance your ability to work with a wide range of characters

Print Unicode Characters in Python

Below are the ways by which we can print unicode characters in Python:

  • Using Unicode Escape Sequences
  • Utilizing the ord() Function
  • Using Unicode Strings
  • Unicode Characters in String Format

Using Unicode Escape Sequences

In this example, the simplest method to print Unicode characters in Python involves using Unicode escape sequences. For example, to print the heart symbol (❤), you can use the escape sequence “\ ,m ,mnb hg”.

Python3




print("\u2764")


Output

Print Unicode Character Utilizing the ord() Function

The ord() function in Python returns the Unicode code point for a given character. By combining ord() with the chr() function, you can easily print Unicode characters. For instance, to print the output, you can do the following:

Python3




print(chr(8364))


Output

Print Unicode Character Using Unicode Strings

Python allows you to directly use Unicode characters in strings. This method is particularly convenient when dealing with a small number of characters.

Python3




print("Hello, this is a snowflake: ❄")


Output

Hello, this is a snowflake: ❄


Unicode Characters in String Format

For more complex scenarios involving a mix of regular and Unicode characters within a string, you can use the encode() method to print Unicode characters. Here’s an example:

Python3




unicode_str = "Unicode character: \u2728"
print(unicode_str.encode('utf-8').decode('unicode_escape'))


Output

Unicode character: ✨


Conclusion

Printing Unicode characters in Python is a valuable skill, especially when working with diverse text and symbols. By mastering these five simple methods, you’ll be equipped to handle a wide range of Unicode characters in your Python projects. Experiment with these techniques to enhance your programming capabilities and embrace the richness of Unicode in your code.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads