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:
a.remove(p)

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!

A little help.

>>> import string
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> 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 5 Years Ago 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.
http://docs.python.org/library/re.html

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 5 Years Ago by snippsat: n/a

This article has been dead for over six months. Start a new discussion instead.