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!