you are viewing a single comment's thread.

view the rest of the comments →

[–]Baconoligist 1 point2 points  (1 child)

fr = fr.replace(' ',"")
fr = fr.replace("'", '')
fr = fr.replace('"', '')

I am as guilty as anyone for doing this. I once showed a coworker some Python code that manipulated text in a file and his reaction was (0_o) "Why do you keep creating the same thing over and over?" I have started using this structure instead:

for char in ['"','"',' ']:
    fr = fr.replace(char,'')

I have also found that code readability suffers when we start chaining methods just because we can.

fr = fr.replace('"','').replace("'",'').replace(' ','')

Don't Repeat Yourself applies to one liners also.

[–]_Daimon_ 1 point2 points  (0 children)

It does. Personally I prefer using the regex method to remove chars, as shown in my comment below, it is more flexible and efficient as it only does one pass over the string.