Share a method of traversing strings (including Chinese characters) using python

Y2J
Release: 2017-05-02 15:01:50
Original
2025 people have browsed it

This article mainly introduces relevant information about python traversing strings (including Chinese characters) and detailed examples. Friends who need it can refer to

python traversing strings (including Chinese characters) detailed examples

s = "中国china"
for j in s:
  print j
Copy after login

First of all, what is the encoding of your 'a'? It may not be what you think gbk

>>> a='中国'
>>> a
Copy after login

Try this. If it comes out with 6 words, it means it is utf-8. If it comes out with 4 words, it means gbk.

In addition, whether it is utf-8 or gbk, it cannot be traversed in this way, because it will be taken out word by word here. The virtual machine treats a as a string with a length of len(a).

The next step is the traversal problem.

Most Linux shells default to utf-8, so one Chinese character is three words, so you have to read it three by three. You can try:

>>> a[:3]
Copy after login

Come out It's just a "中" character

The default command of windows is cp936, which is gbk. One Chinese character is two characters, so two characters are read two characters (a[:2] ).

There is another way to traverse, convert the string into unicode, so that both Chinese and English are one word, and you can use your for i in a method to traverse. The advantage of this is that Chinese and English characters are all one word, while in utf-8 and gbk, English letters only occupy one word.

s = u"中国china"
for j in s:
  print j
Copy after login

The output is as follows:

中
国
c
h
i
n
a
Copy after login

The above is the detailed content of Share a method of traversing strings (including Chinese characters) using python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template