Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9131895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:13:08+00:00 2026-06-17T08:13:08+00:00

Possible Duplicate: In Python, how to list all characters matched by POSIX extended regex

  • 0

Possible Duplicate:
In Python, how to list all characters matched by POSIX extended regex `[:space:]`?

How can I get a list of all whitespaces in UTF-8 in Python? Including non-breaking space etc. I’m using python 2.7.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:13:09+00:00Added an answer on June 17, 2026 at 8:13 am

    unicodedata.category will tell you the category code for any given character; the characters you want have code Zs. There doesn’t appear to be any way to extract a list of the characters within a category except by iterating over all of them:

    >>> for c in xrange(sys.maxunicode+1):
    ...     u = unichr(c)
    ...     if unicodedata.category(u) == 'Zs':
    ...         sys.stdout.write("U+{:04X} {}\n".format(c, unicodedata.name(u)))
    ... 
    U+0020 SPACE
    U+00A0 NO-BREAK SPACE
    U+1680 OGHAM SPACE MARK
    U+180E MONGOLIAN VOWEL SEPARATOR
    U+2000 EN QUAD
    U+2001 EM QUAD
    U+2002 EN SPACE
    U+2003 EM SPACE
    U+2004 THREE-PER-EM SPACE
    U+2005 FOUR-PER-EM SPACE
    U+2006 SIX-PER-EM SPACE
    U+2007 FIGURE SPACE
    U+2008 PUNCTUATION SPACE
    U+2009 THIN SPACE
    U+200A HAIR SPACE
    U+202F NARROW NO-BREAK SPACE
    U+205F MEDIUM MATHEMATICAL SPACE
    U+3000 IDEOGRAPHIC SPACE
    

    (Note: if you do this test using Python 3.4 or later, MONGOLIAN VOWEL SEPARATOR will not appear in the list. Python 2.7 shipped with data from Unicode 5.2; this character was reclassified as general category Cf (“formatting control”) in Unicode 6.3, which is the version that Python 3.4 used for its data. See https://codeblog.jonskeet.uk/2014/12/01/when-is-an-identifier-not-an-identifier-attack-of-the-mongolian-vowel-separator/ and https://www.unicode.org/L2/L2013/13004-vowel-sep-change.pdf for more detail than you probably require.)

    You may also want to include categories Zl and Zp, which adds

    U+2028 LINE SEPARATOR
    U+2029 PARAGRAPH SEPARATOR
    

    And you almost certainly do want to include all of the ASCII control characters that are normally considered whitespace — for historical reasons (I presume), these are in category Cc.

    U+0009 CHARACTER TABULATION  ('\t')
    U+000A LINE FEED (LF)        ('\n')
    U+000B LINE TABULATION       ('\v')
    U+000C FORM FEED (FF)        ('\r')
    U+000D CARRIAGE RETURN (CR)  ('\f')
    

    The other 60-odd Cc characters should not be considered whitespace, even if their official name makes it sound like they are whitespace. For instance, U+0085 NEXT LINE is almost never encountered in the wild with its official meaning; it’s far more likely to be the result of an erroneous conversion from Windows-1252 to UTF-8 of U+2026 HORIZONTAL ELLIPSIS.

    A closely-related question is “what does \s match in a Python regular expression?” Again the best available way to answer this question is to iterate over all characters:

    >>> s = re.compile(ru"^\s$", re.UNICODE)
    >>> for c in range(sys.maxunicode+1):
    ...   u = unichr(c)
    ...   if s.match(u):
    ...      sys.stdout.write("U+{:04X} {}\n".format(
    ...        c, unicodedata.name(u, "<name missing>")))
    U+0009 <name missing>
    U+000A <name missing>
    U+000B <name missing>
    U+000C <name missing>
    U+000D <name missing>
    U+001C <name missing>
    U+001D <name missing>
    U+001E <name missing>
    U+001F <name missing>
    U+0020 SPACE
    U+0085 <name missing>
    U+00A0 NO-BREAK SPACE
    U+1680 OGHAM SPACE MARK
    U+180E MONGOLIAN VOWEL SEPARATOR
    U+2000 EN QUAD
    U+2001 EM QUAD
    U+2002 EN SPACE
    U+2003 EM SPACE
    U+2004 THREE-PER-EM SPACE
    U+2005 FOUR-PER-EM SPACE
    U+2006 SIX-PER-EM SPACE
    U+2007 FIGURE SPACE
    U+2008 PUNCTUATION SPACE
    U+2009 THIN SPACE
    U+200A HAIR SPACE
    U+2028 LINE SEPARATOR
    U+2029 PARAGRAPH SEPARATOR
    U+202F NARROW NO-BREAK SPACE
    U+205F MEDIUM MATHEMATICAL SPACE
    U+3000 IDEOGRAPHIC SPACE
    

    (I don’t know why unicodedata.name doesn’t know the control characters’ names. Again, if you do this test using Python 3.4 or later, MONGOLIAN VOWEL SEPARATOR will not appear in the list.)

    This is all of the Z* characters, all of the Cc characters that are generally agreed to be whitespace, and five extra characters that are not generally agreed to be whitespace, U+001C, U+001D, U+001E, U+001F, and U+0085. Inclusion of the last group is a bug, but a largely harmless one, since using those characters for anything is also a bug.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Python code to pick out all possible combinations from a list? I
Possible Duplicate: How to generate all permutations of a list in Python I want
Possible Duplicate: Python “extend” for a dictionary I know that Python list can be
Possible Duplicate: Python Check if all of the following items is in a list
Possible Duplicate: Fastest way to list all primes below N in python Although I
Possible Duplicate: Python list subtraction operation In Python you can concatenate lists like so:
Possible Duplicate: Inspect python class attributes I need to get a clean list of
Possible Duplicate: How to generate all permutations of a list in Python I am
Possible Duplicate: Fastest way to list all primes below N in python I have
Possible Duplicate: Unable to get a list of installed Python modules How do I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.