Implement a function punctuation() that takes no parameters, inputs a string from the user, and prints all of the punctuation characters appearing in the string, in order from left to right.

My coding I have so far is:

def punctuation():
a = raw_input("Please enter a string:")
check =
for p in check:
if p in a:

I'm working on IDLE and I keep getting an error message saying raw_input isn't defined? Any help you guys could give me is appreciated!

7 Years
Discussion Span
Last Post by snippsat

A little help.

>>> import string
>>> string.ascii_letters
>>> string.punctuation

In this code i exclude string.ascii_letters

import string

exclude = string.ascii_letters
s = 'test.. hi? for ,,,'
punc_text = ''.join(ch for ch in s if ch not in exclude)
print punc_text #.. ?  ,,,

I keep getting an error message saying raw_input isn't defined?

If you are using python 3 change raw_input() to input()
a.remove(p) will never work because string method dos not have a remove method.

Edited by snippsat: n/a


You can also use re to remove all characters with the W flag.

Just to give a little more info about this.
richieking is talking about regular expression.

Her is a demo of how it work.
Using findall is an easy option,search and match are to other option to read about.

>>> import re
>>> r = re.findall(r'\W', 'test.. hi? for ,,,')
>>> r
['.', '.', ' ', '?', ' ', ' ', ',', ',', ',']
>>> #Make it a string
>>> ''.join(r)
'.. ?  ,,,'

Edited by snippsat: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.