0

I tried to use string.punctuation function in python.I doing it in python 3.5 version. This is my following code:

import string
def check(text):
    x=set(text.punctuation())

I am getting following error

'str' object has no attribute 'punctuation'

PLease help with the correct way of using this.I even tried using ispunct() fucntion.

I tried for solving this as shown in the link below,but couldn't figure it out.

Best way to strip punctuation from a string in Python

Community
  • 1
  • 1
Priyaranjan
  • 407
  • 3
  • 9
  • 16

1 Answers1

6

punctuation is a method of string module not a string object attribute.

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

If you want to get the punctuation from a text you can use a generator expression within set :

set(i for i in text if i in string.punctuation)
Mazdak
  • 105,000
  • 18
  • 159
  • 188