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!

Recommended Answers

All 3 Replies

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.

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

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)
'.. ?  ,,,'
>>>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.