Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complete set of punctuation marks for Python (not just ASCII)

Is there a listing or library that has all punctuations that we might commonly come across?

Normally I use string.punctuation, but some punctuation characters are not included in it, for example:

>>> "'" in string.punctuation True >>> "’" in string.punctuation False 
like image 699
samuelbrody1249 Avatar asked Apr 02 '20 03:04

samuelbrody1249


People also ask

How do I get all the punctuations in Python?

In Python, string. punctuation will give the all sets of punctuation. Parameters : Doesn't take any parameter, since it's not a function. Returns : Return all sets of punctuation.

Are punctuation marks used in Python?

A Python string may contain letters, whitespace, numbers, and punctuation. Punctuation characters include commas and periods and semicolons. With Python, we can access the string.

Which Python module includes a list of punctuations?

punctuation string. The string punctuation is pre-defined in the string module of Python3. It contains all the characters as a string. We can use it anywhere in the program.

What is ASCII punctuation?

ASCII is a code set containing 128 code points (0x00 through 0x7F). The ASCII character set contains control characters, punctuation marks, digits, and the uppercase and lowercase English alphabet. Several 8-bit code sets incorporate ASCII as a proper subset.


2 Answers

You might do better with this check:

>>> import unicodedata >>> unicodedata.category("'").startswith("P") True >>> unicodedata.category("’").startswith("P") True 

The Unicode categories P* are specifically for Punctuation:

connector (Pc), dash (Pd), initial quote (Pi), final quote (Pf), open (Ps), close (Pe), other (Po)

To prepare the exhaustive collection, which you can subsequently use for fast membership checks, use a set comprehension:

>>> import sys >>> from unicodedata import category >>> codepoints = range(sys.maxunicode + 1) >>> punctuation = {c for i in codepoints if category(c := chr(i)).startswith("P")} >>> "'" in punctuation True >>> "’" in punctuation True 

Assignment expression here requires Python 3.8+, equivalent for older Python versions:

chrs = (chr(i) for i in range(sys.maxunicode + 1)) punctuation = set(c for c in chrs if category(c).startswith("P")) 

Beware that some of the other characters in string.punctuation are actually in Unicode category Symbol. It's easy to add those in also if you want.

like image 183
wim Avatar answered Sep 16 '22 15:09

wim


The answer posted by wim is correct if you want to check if a character is a punctuation character.

If you really need a list of all punctuation characters as your question title suggests, you can use the following:

import sys from unicodedata import category punctuation_chars =  [chr(i) for i in range(sys.maxunicode)                               if category(chr(i)).startswith("P")] 
like image 23
Selcuk Avatar answered Sep 17 '22 15:09

Selcuk