Python Forum
strip space from end of a row of text - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: strip space from end of a row of text (/thread-17521.html)



strip space from end of a row of text - ineuw - Apr-14-2019

I am using Linux Mint 19.1 and Python 3.6.

Proofreading pages of text on the web, where each row ends with a space, followed by a newline. I need to replace (strip) the space at the end of the row.

My first question is: Is it necessary to iterate through the rows one by one, or can all rows be stripped at once by using .replace() ?

I tested the following code but it doesn't work.

import time

keyboard.send_keys("<ctrl>+a")
time.sleep(.5)
text = clipboard.get_selection() 
old = " " + "\n"
new = "\n"
text.replace(old, new)
keyboard.send_keys(text)
Also tried .strip() and that doesn't work either.


RE: strip space from end of a row of text - ichabod801 - Apr-14-2019

String methods don't work in place, they return a modified version of the string. You need to assign that result back to text.


RE: strip space from end of a row of text - ineuw - Apr-14-2019

(Apr-14-2019, 07:01 PM)ichabod801 Wrote: String methods don't work in place, they return a modified version of the string. You need to assign that result back to text.

Thanks ichabod801. How can I see the modifed returns?


RE: strip space from end of a row of text - ichabod801 - Apr-15-2019

text = text.replace(old, new)


RE: strip space from end of a row of text - ineuw - Apr-15-2019

(Apr-15-2019, 12:44 AM)ichabod801 Wrote: text = text.replace(old, new)

Much thanks ichabod801. It works fine.