Sling Academy
Home/Python/Python: Convert character to Unicode code point and vice versa

Python: Convert character to Unicode code point and vice versa

Last updated: June 01, 2023

This concise and straight-to-the-point article will show you how to convert a character to a Unicode code point and turn a Unicode code point into a character in Python.

What are Unicode code points?

A Unicode code point is a numerical value that represents a specific character in the Unicode standard. It is a unique identifier assigned to each character and can range from 0 to 0x10FFFF.

The Unicode code point allows for the universal representation and encoding of characters from various writing systems and languages. It provides a standardized way to represent and process text in different scripts and symbols.

Convert a character to a Unicode code point in Python

Python provides a function named ord(), which takes a character as input and returns its corresponding Unicode code point. This is the tool we’ll use to get the job done.

Example:

character = 'A'
code_point = ord(character)
print(f"The code point of '{character}' is {code_point}")

character = '😄'
code_point = ord(character)
print(f"The code point of '{character}' is {code_point}")

Output:

The code point of 'A' is 65
The code point of '😄' is 128516

If you inadvertently (or intentionally) provide an invalid input to the ord() function, it will not be happy and raise a ValueError as shown below:

character = 'ABC_DEF'
code_point = ord(character)
# TypeError: ord() expected a character, but string of length 7 found

Convert a Unicode code point to a character in Python

In order to convert a Unicode code point to its corresponding character, you can use the Python built-in chr() function.

Example:

code_point = 102
character = chr(code_point)
print(f"The character corresponding to code point {code_point} is '{character}'")

Output:

The character corresponding to code point 102 is 'f'

What if you feed the chr() function with an invalid, out-of-range input (the valid range is from 0 to 0x10FFFF)? Well, it will raise a ValueError as the ord() function does, like this:

code_point = 10222111
character = chr(code_point)

# ValueError: chr() arg not in range(0x110000)

Next Article: Python: 3 Ways to Check If a String Is a Valid URL

Previous Article: 2 ways to get the size of a string in Python (in bytes)

Series: Working with Strings in Python

Python

You May Also Like

  • Introduction to yfinance: Fetching Historical Stock Data in Python
  • Monitoring Volatility and Daily Averages Using cryptocompare
  • Advanced DOM Interactions: XPath and CSS Selectors in Playwright (Python)
  • Automating Strategy Updates and Version Control in freqtrade
  • Setting Up a freqtrade Dashboard for Real-Time Monitoring
  • Deploying freqtrade on a Cloud Server or Docker Environment
  • Optimizing Strategy Parameters with freqtrade’s Hyperopt
  • Risk Management: Setting Stop Loss, Trailing Stops, and ROI in freqtrade
  • Integrating freqtrade with TA-Lib and pandas-ta Indicators
  • Handling Multiple Pairs and Portfolios with freqtrade
  • Using freqtrade’s Backtesting and Hyperopt Modules
  • Developing Custom Trading Strategies for freqtrade
  • Debugging Common freqtrade Errors: Exchange Connectivity and More
  • Configuring freqtrade Bot Settings and Strategy Parameters
  • Installing freqtrade for Automated Crypto Trading in Python
  • Scaling cryptofeed for High-Frequency Trading Environments
  • Building a Real-Time Market Dashboard Using cryptofeed in Python
  • Customizing cryptofeed Callbacks for Advanced Market Insights
  • Integrating cryptofeed into Automated Trading Bots