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?

Legato
9,9294 gold badges50 silver badges118 bronze badges
asked Nov 11, 2015 at 2:29
\$\endgroup\$
5
  • \$\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\$ Commented Nov 11, 2015 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\$ Commented Nov 11, 2015 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\$ Commented Nov 11, 2015 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\$ Commented Nov 11, 2015 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\$ Commented Nov 11, 2015 at 5:41

3 Answers 3

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

answered Nov 11, 2015 at 10:46
\$\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.

answered Nov 11, 2015 at 16:48
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.