Unicode Tutorials - Herong's Tutorial Examples - v5.32, by Herong Yang
Unicode Support on "str" Data Type
This section provides tutorial example on how to use the built-in data type 'str' to store Unicode characters as a list of code points.
When using Python built-in data type "str" to store Unicode characters, you need to remember that:
Here is a Python script that demonstrates how to use data type "str" to store Unicode character strings:
# str-Data-Type-on-Unicode.py # Copyright 2019 (c) HerongYang.com. All Rights Reserved. # # "str" literals are decoded as UTF-8 encoded bytes by default str = "Français" # len(str) returns the number of code points in a "str" num = len(str) # a "str" can be accessed as a list of Unicode code points for i in range(num): char = str[i] code = ord(char) print("{0} - {1}".format(hex(code), code)) # \N{}, \x, \u and \U escape sequences can be used in "str" literals print("Français") print("Fran\N{LATIN SMALL LETTER C WITH CEDILLA}ais") print("Fran\xe7ais") print("Fran\u00e7ais") print("Fran\U000000e7ais") # one character can be constructed with 2 combining code points one = "\N{LATIN SMALL LETTER C WITH CEDILLA}" two = "\N{LATIN SMALL LETTER C}\N{COMBINING CEDILLA}" print("{0} with {1} code point".format(one, len(one))) print("{0} with {1} code points".format(two, len(two)))
Run the above script, it will print the following output:
herong$ python3 str-Data-Type-on-Unicode.py 0x46 - 70 0x72 - 114 0x61 - 97 0x6e - 110 0xe7 - 231 0x61 - 97 0x69 - 105 0x73 - 115 Français Français Français Français Français ç with 1 code points ç with 2 code points
Table of Contents
ASCII Character Set and Encoding
GB2312 Character Set and Encoding
GB18030 Character Set and Encoding
JIS X0208 Character Set and Encodings
UTF-8 (Unicode Transformation Format - 8-Bit)
UTF-16, UTF-16BE and UTF-16LE Encodings
UTF-32, UTF-32BE and UTF-32LE Encodings
►Python Language and Unicode Characters
Summary of Unicode Support in Python
►Unicode Support on "str" Data Type
Unicode Character Encoding and Decoding
"unicodedata" Module for Unicode Properties
Java Language and Unicode Characters
Encoding Conversion Programs for Encoded Text Files
Using Notepad as a Unicode Text Editor
Using Microsoft Word as a Unicode Text Editor