# Removing punctuation and lowercasing a string

I am very fresh to python. As part of an assignment, I've written the following code to remove punctuation from a string and convert it to lowercase.

import string

translation = str.maketrans("","", string.punctuation);
new = words.translate(translation);
lower = new.lower();


However, this seems crude to me and I feel like I can accomplish my task with fewer function calls/less code. Does anyone have any suggestions as to how I could do this?

• I mean I've done the tasked asked of me no problem, and this is maybe a quarter of it. I'm simply asking if theres a cleaner method to it. This'll suffice just fine for my class. – David Tamrazov Nov 11 '15 at 2:38
• This question is on the line for off-topic category right now, and I am not sure what to do with it. It seems to be unfinished, so I will vote to close this question. – TheCoffeeCup Nov 11 '15 at 3:36
• This doesn't look too off-topic to me. It's fully functional code, and David is asking for improvements. – Nic Hartley Nov 11 '15 at 5:00
• There is absolutely nothing wrong with this. OP has accomplished his task, and is asking how could it be better. This is 100% what CR is about. We wouldn't have a homework tag if this wasn't acceptable. – Legato Nov 11 '15 at 5:39
• There is a bit of a mess, though. The original title mentioned splitting the string, which the code never actually does. Yet @PatrickS has incorporated splitting into his answer. – 200_success Nov 11 '15 at 5:41

To reduce the number of lines and statements for your problem note:

• For your input format splitting on spaces and removing punctuation can be a single operation: split on , (comma-space).
• You don't need to use a (new) variable for every intermediate step.

String methods can be chained. Here words is first made lower-case and then splitis called.

words = "Dave, Laura, Maddy, Dave, Laura, Maddy, Dave, Laura, Dave";
result =  words.lower().split(", ")


Alternatively you can use a list comprehension. Here the words are split and then every word of the intermediate result is made lower-case.

words = "Dave, Laura, Maddy, Dave, Laura, Maddy, Dave, Laura, Dave";
result = [word.lower() for word in words.split(", ")]

• Your answer returns a result, while I think OP just wants a string as output? – kushj Nov 11 '15 at 5:49
• Oh brilliant, that's so much cleaner. Thats exactly the kind of revision I was looking for, thanks @PatrickS. – David Tamrazov Nov 11 '15 at 7:36
• @kushj No I actually need a string to work with. – David Tamrazov Nov 11 '15 at 7:37

Instead of just import string you could get the punctuation list directly:

from string import punctuation

translation = str.maketrans("", "", punctuation)


I'd also remove the semicolons. Python doesn't need them to know when a line of code ends since it uses newlines instead (there are exceptions, but none here) so you can remove them.

Here's how I would do it:

Mesh the following operations into a single line:

 - step 1: str.split(",")  # split string into a list, remove "," chars
- step 2: "".join(lst)    # join lst items with "" in between
- step 3: str.lower()     # turn string to lowercase


res = "".join(words.split(",")).lower()