How to handle multibyte string in Python

后端 未结 2 1325
闹比i
闹比i 2020-12-19 10:32

There are multibyte string functions in PHP to handle multibyte string (e.g:CJK script). For example, I want to count how many letters in a multi bytes string by using

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-19 10:56

    Use Unicode strings:

    # Encoding: UTF-8
    
    japanese = u"桜の花びらたち"
    print japanese
    print len(japanese)
    

    Note the u in front of the string.

    To convert a bytestring into Unicode, use decode: "桜の花びらたち".decode('utf-8')

提交回复
热议问题