Python chr() Function – Tutorial with Examples

The chr() function in Python is a built-in function that returns a string representing a character whose Unicode code point is the integer parameter. It takes an integer (decimal or hexadecimal) as input and returns a string of length 1 that contains the corresponding Unicode character.

Syntax

chr(i)

i – the integer representing the Unicode code point of the character to be returned.

Return value

The chr() function returns a string of length 1 containing the Unicode character corresponding to the input integer code point.

Examples

Example 1: Return the character for a decimal code point

print(chr(97))

Output: 'a'

Example 2: Return the character for a hexadecimal code point

print(chr(0x41))

Output: 'A'

Example 3: Return the character for a code point outside the ASCII range

print(chr(0x1F600))

Output: '?'

Use Cases

The chr() function is commonly used in Python for the following purposes:

  • Converting code points to characters in Unicode
  • Generating strings containing special characters
  • Decoding and encoding text in different character sets

In conclusion, the chr() function is a useful tool for working with Unicode characters in Python. It takes an integer representing a Unicode code point and returns the corresponding character as a string of length 1. This function can be used for various purposes, including generating strings with special characters, decoding and encoding text, and more.

Leave a Reply

Your email address will not be published. Required fields are marked *