문제

How can I access Python's string constants ?

I need to get a list of punctuation marks which Python docs say are in string.punctuation

This won't work

''.punctuation
''.get_value("punctuation")

Is what I'm trying even possible?

도움이 되었습니까?

해결책

string in string.punctuation isn't referring to type str, but to a string module:

In [32]: import string

In [33]: string.punctuation
Out[33]: '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

다른 팁

Like so:

>>> import string
>>> string
<module 'string' from '/usr/lib/python2.7/string.pyc'>
>>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

These are variables in the module so you do not need an instance use the module name. Note for others I assume the python docs referred to in the question are here
e.g.

import string
print(string.punctuation)

gives

!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top