Tips for sharing Python Chinese Characters

Source: Internet
Author: User

There are many useful operations in the Python programming language that can help us easily implement functions in certain environments. For example, you can operate on Chinese characters. Today, let's take a look at the application skills related to Chinese Characters in Python.

Python-related operation code example:

 
 
  1. #! /Usr/bin/python
  2. #-*-Coding: UTF-8 -*-
  3. S = "China"
  4. Ss = u "China"
  5. Print s, type (s), len (s)
  6. Print ss, type (ss), len (ss)
  7. Print '-' * 40
  8. Print repr (s)
  9. Print repr (ss)
  10. Print '-' * 40
  11. Ss1 = s. decode ('utf-8 ')
  12. Print s1, len (s1), type (s1)
  13. Print '-' * 40
  14. Ss2 = s. decode ('utf-8'). encode ('gbk ')
  15. Print s2
  16. Print type (s2)
  17. Print len (s2)
  18. Print '-' * 40
  19. S3 = ss. encode ('gbk ')
  20. Print s3
  21. Print type (s3)
  22. Print len (s3)

The execution result is as follows:

 
 
  1. China <type 'str'> 6
  2. China <type 'unicode '> 2
  3. ----------------------------------------
  4. '\ Xe4 \ xb8 \ xad \ xe5 \ x9b \ xbd'
  5. U' \ u4e2d \ u56fd'
  6. ----------------------------------------
  7. China 2 <type 'unicode '>
  8. ----------------------------------------
  9. ��
  10. <Type 'str'>
  11. 4
  12. ----------------------------------------
  13. ��
  14. <Type 'str'>
  15. 4

Supplement:

View the default encoding settings for Chinese Characters in Python:

 
 
  1. >>> import sys  
  2. >>> sys.getdefaultencoding()  
  3. 'ascii' 

Because #-*-coding: UTF-8-*-is specified on the file header, the encoding of s is UTF-8.

  • Python file path
  • Python exception handling mechanism
  • Analysis of basic Python String applications
  • Summary of Python Process Control keywords
  • Introduction to two common methods for connecting Python to a database

 

In UTF-8, English letters are one byte, and Chinese characters are three bytes;

The Chinese Character in unicode is 1 character dubyte );

The Chinese Character in GBK encoding occupies 2 bytes. (Thanks to keakon for correcting)

The preceding section describes the Chinese characters of Python.

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.