Python Forum

Full Version: strip space from end of a row of text
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
String methods don't work in place, they return a modified version of the string. You need to assign that result back to text.
(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?
text = text.replace(old, new)
(Apr-15-2019, 12:44 AM)ichabod801 Wrote: [ -> ]text = text.replace(old, new)

Much thanks ichabod801. It works fine.