Python - Convert String to unicode characters

Python - Convert String to unicode characters

In this tutorial, we'll learn how to convert a string into its corresponding Unicode (code point) representation in Python.

Objective:

Convert each character in a string to its Unicode code point.

For Example: Given the string:

string = "hello" 

The conversion should produce:

[104, 101, 108, 108, 111] 

Step-by-step Solution:

1. Use the ord() Function:

The built-in Python function ord() returns an integer representing the Unicode character.

unicode_val = ord('h') # This will return 104 

2. Convert the Entire String:

Using a list comprehension, iterate over the string and convert each character to its Unicode value.

unicode_list = [ord(char) for char in string] 

Complete Code:

Here's the complete code to convert a string into its Unicode representation:

# Sample string string = "hello" # Convert string to list of Unicode values unicode_list = [ord(char) for char in string] print(unicode_list) 

When executed, the code will produce:

[104, 101, 108, 108, 111] 

Additional Notes:

  • Unicode provides a unique number for every character, no matter the platform, device, application, or language.

  • The ord() function works for a single character string. It returns the Unicode code point for that character.

  • If you want to convert Unicode values back to their character representations, you can use the chr() function:

    char_representation = chr(104) # This will return 'h' 

Through this tutorial, you've learned how to convert each character in a string into its corresponding Unicode representation in Python. This knowledge is particularly useful when dealing with internationalized applications or performing certain types of text processing or encryption.


More Tags

docker-desktop absolute-path checkboxlist pentaho-spoon deterministic xcode4.5 django-channels delivery-pipeline mongodb-atlas openjpa

More Programming Guides

Other Guides

More Programming Examples