Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Davis Rousseau
6,670 Pointss.translate( table )
Not understanding what I'm doing wrong. Getting an error
Write a sequence of statements that produce a copy of s, named newS, in which characters.,,,;,and\n have been replaced by blank spaces
My answer
newS = str.maketrans(‘.,,,;,\n’, ‘ ‘)
s = '''It was the best of times, it was the worst of times; it was the age of wisdom, it was the age of foolishness; it was the epoch of belief, it was the epoch of incredulity; it was...’''
print s.translate(newS)
6 Answers
Kenneth Love
Treehouse Guest TeacherSo, two solutions. I prefer the latter.
Fixing your solution
newS = str.maketrans('.,;\n', ' ')
print(s.translate(newS))
You get a string with a bunch of extra spaces in it. That's why I'm not a fan of it, the resulting string is silly.
Cleaner but no trailing space
newS = str.maketrans('', '', '.,;\n')
print(s.translate(newS))
Results in the same string but with no extra spaces and no trailing space (for the \n).
This is my preferred solution because it gives us a string that actually makes sense.
Bonus!
newS = s
for character in '.,;\n':
newS = newS.replace(character, ' ')
This gives you the requested version, or change ' ' to '' and you get my cleaner version. Yes, this is more code than either of the above two versions but it's a bit more explicit, too, instead of relying on the somewhat unknown translate and maketrans.
Miguel de Luis Espinosa
41,279 PointsDid you remember to import the module maketrans ?
Check if you are using python2 or 3 Your code won't work on python 3 (you need print(whatever) not print whatever)
Also, why do you need to repeat so many commas in the intab?
Kenneth Love
Treehouse Guest TeacherYour existing answer has newS as the translator, not as a string. You probably want something like:
translator = str.maketrans('.,;\n', ' ')
newS = s.translate(translator)
newS.lower()
You could combine lines 2 and 3.
Davis Rousseau
6,670 PointsI'm using python 3. I removed the commas and now only using one. Kenneth the problem I'm working on wants the leading and trailing blank spaces. The next problem wants me to remove it. thanks again.
Davis Rousseau
6,670 PointsTrying to figure this out for hours now continuation to this problem
Question: Make the characters in newS lowercase (and name the new string newS)
Answer:
s.lower() 'it was the best of times, it was the worst of times; it was the age of wisdom, it was the age of foolishness; it was the epoch of belief, it was the epoch of incredulity; it was...'
but that remove the first step I already made with the character changes.
Davis Rousseau
6,670 PointsWhy did you name it translator?
Kenneth Love
Treehouse Guest TeacherBecause you want newS to be a string, not the translation object...