Python character encoding

Source: Internet
Author: User
1 >>> str_ascii = 'Hello! '
2 >>> str_unicode = U' hello! '
3 >>> str_utf8 = str_unicode.encode ('utf-8 ')
4 >>> str_ascii
5' \ xc4 \ xe3 \ xba \ xc3! '
6 >>> str_unicode
7 U' \ u4f60 \ u597d! '
8 >>> str_utf8
9' \ xe4 \ xbd \ xa0 \ xe5 \ xa5 \ xbd! '
10 >>> str_utf8.decode ('utf-8 ')
11 U' \ u4f60 \ u597d! '
12 >>> str_unicode.encode ('gbk ')
13' \ xc4 \ xe3 \ xba \ xc3! '

 

What is the conclusion?

  1. Unicode is 1 character and 1 encoding for any language, so "hi !" It is a 3-character Unicode character. "Hello !" It is also a three-character Unicode character.
  2. The UTF-8 represents 1 Chinese character with 3 Characters and 1 English character. So, "hi !" Is a 3 UTF-8 character, "Hello !" Is a 7 UTF-8 character.
  3. GBK uses two characters to represent one Chinese Character and one character to represent an English character. So, "hi !" It is 3 GBK characters. "Hello !" Is 5 GBK characters.
  4. The encode method is only applicable to unicode strings. The parameter is the target encoding code.
  5. The decode method is applicable only to non-unicode strings. The parameter is the encoding code of the source string.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.