Examples of python traversal strings (including Chinese characters)
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
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!