2
\$\begingroup\$

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

words = "Dave, Laura, Maddy, Dave, Laura, Maddy, Dave, Laura, Dave";
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?

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – David Tamrazov Nov 11 '15 at 2:38
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – TheCoffeeCup Nov 11 '15 at 3:36
  • \$\begingroup\$ This doesn't look too off-topic to me. It's fully functional code, and David is asking for improvements. \$\endgroup\$ – Nic Hartley Nov 11 '15 at 5:00
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Legato Nov 11 '15 at 5:39
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – 200_success Nov 11 '15 at 5:41
1
\$\begingroup\$

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(", ")]
\$\endgroup\$
  • \$\begingroup\$ Your answer returns a result, while I think OP just wants a string as output? \$\endgroup\$ – kushj Nov 11 '15 at 5:49
  • \$\begingroup\$ Oh brilliant, that's so much cleaner. Thats exactly the kind of revision I was looking for, thanks @PatrickS. \$\endgroup\$ – David Tamrazov Nov 11 '15 at 7:36
  • \$\begingroup\$ @kushj No I actually need a string to work with. \$\endgroup\$ – David Tamrazov Nov 11 '15 at 7:37
2
\$\begingroup\$

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

from string import punctuation

words = "Dave, Laura, Maddy, Dave, Laura, Maddy, Dave, Laura, Dave"
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.

\$\endgroup\$
1
\$\begingroup\$

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

Your desired code:

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

p.s. best I am aware, join() is faster than list comprehension.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.