Use python to count double-byte spaces at the beginning of the string.

I want to do the above with the UTF-8 character code.

If i just normally want to count all double-byte spaces,

str = '2019/11/2 today's date'
pritnt str.count ('\ u3000')

Will return.


From "2019/11/2 today's date"
This is the number of full-width blank strings before 2019/11/2


I want to return.
Can you tell me if there is any good way to do it?

  • Answer # 1

    >>>import re
    >>>s = '2019/11/2 today's date'
    >>>re.match (r "\ u3000 *", s) .end ()

    re.matchis conveniently used to match only at the beginning of the string.
    re --- Regular expression manipulation — Python 3.8.0 documentation


    raw-strings is used,\ u3000is recognized as a double-byte space by the regular expression parser, and since*is used, it must match (If the first character is not a full-width space, a match object ofspan = (0, 0)will be returned) is added as a reminder.

  • Answer # 2

    An easy-to-understand method is to use a single character slice and look at the first character in order, and count until it is not a full-width space character or the string is exhausted. is.

  • Answer # 3

    This is the way I came up with.

    src = '2019/11/2 today's date'
    print (
        len (src)-len (src.lstrip (''))

  • Answer # 4

    It's a bit verbose in terms of processing, but ...

    >>>import re
    >>>str = '2019/11/2 today's date'
    >>>print (len (re.split ('[^ \ u3000]', str) [0]))

  • Answer # 5

    >>>import re
    >>>str = '2019/11/2 today's date'
    >>>re.search (r '[^ \ u3000]', str) .start ()