Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Convert Integer to Unicode Character in Python



Unicode is a standardized character encoding that assigns a unique number to each character in most of the world's writing systems. Unicode separates the code points from the details of the encoding system.

This permits a much wider range of characters up to four bytes. The Unicode character set incorporates the entirety of the ASCII character set as the first 127 characters. All ASCII characters have the same code points in both encodings.

Techniques to Convert an Integer to a Character

Following are the various techniques to convert an integer to a character in Python ?

Using chr() Function

In Python, the chr() is used to convert an integer between 0 and 0x10ffff to a corresponding Unicode character in the ASCII table.

Example

Following is an example of converting an integer to a Unicode character using the chr() function ?

print("ASCII value of 36 :",chr(36))

Following is the output of the above code ?

ASCII value of 36 : $

Example

Using the chr() function we can print all the alphabets using a for loop ?

for i in range(97,123):
   print(chr(i), end=" ")

Following is the output of the above code ?

a b c d e f g h i j k l m n o p q r s t u v w x y z

Using format() Function

The format() is a built-in method of the str class. The format() method works by defining placeholders within a string using curly braces {}. These placeholders are then replaced by the values specified in the method's arguments.

Example

In the following example we have converted the integer value to the Unicode using string format() function ?

# Generate Unicode character dynamically using format()
code_point = 97  # Unicode for 'A'
unicode_char = "{:c}".format(code_point)  # 'c' format specifier converts to the character
print("97 unicode character :",unicode_char) 

Following is the output of the above code ?

97 unicode character : a

Using bytes.decode() Method

The bytes method returns an immutable bytes object initialized with the given size and data. Each byte is an integer value between 0 and 255. It is commonly used to store binary data, such as images, files, or network packets. We can create bytes in Python using the built-in bytes() function or by prefixing a sequence of numbers with b. We can convert a bytes object into a string using the .decode() method.

Example

In the following example, we have converted integer to string using bytes.decode() method ?

# For single-byte characters
byte_sequence = bytes([100])  # ASCII for 'd'
unicode_char = byte_sequence.decode('utf-8')
print("ACSII Value of 100 :",unicode_char)

Following is the output of the above code ?

ACSII Value of 100 : d

Using eval() Function with Unicode Escape

The Python eval() function is a built-in function used to evaluate a string as a Python expression and return the result. It takes a string containing a valid Python expression as input, parses it, and executes the code, ultimately returning the expression's result.

Example

Here, we have converted the integer to a Unicode character using the eval() function with Unicode Escape ?

unicode_char = eval('f"\u{:04x}"'.format(65))
print("65 unicode character in ASCII:", unicode_char)

Following is the output of the above code ?

65 unicode character in ASCII : A
Updated on: 2025-01-22T14:30:09+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements