This article describes how to use python to traverse strings (including Chinese characters ).

Source: Internet
Author: User
This article mainly introduces detailed information about python traversal strings (including Chinese characters). For more information, see the next article) for more information about instances, see

Python traversal string (including Chinese characters)

S = "china" for j in s: print j

First, what encoding is your 'A? Maybe not what you think of gbk

>>> A = 'China' >>>

In this case, if it is 6 words (word), it indicates utf-8. if it is 4 words, it indicates gbk.

In addition, both UTF-8 and gbk cannot be traversed in this way, because here it will take out a word. The VM regards a as a string with a length of len (.

Next is the traversal problem.

Most Linux shell statements use UTF-8 by default. Therefore, a Chinese character contains three characters, so it must be read in three places. try again:

>>> A [: 3]

"Medium"

Windows command is cp936 by default, that is, gbk. a Chinese character is two characters, so the two words are read (a [: 2]).

Another traversal method is to convert the string to unicode, so that both Chinese and English words can be traversed using your for I in a method. The advantage is that Chinese and English characters are all one character, while English letters only occupy one word in UTF-8 and gbk.

S = u "china" for j in s: print j

The output is as follows:

China

The preceding section describes how to use python to traverse strings (including Chinese characters). For more information, see other related articles in the first PHP community!

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.