How to remove space from end of line in Python?

I need to remove spaces after a word in a string. Can this be done in one line of code?

Example:

string = " xyz " desired result : " xyz" 
+52
python
Mar 03 '10 at 15:36
source share
2 answers
 >>> " xyz ".rstrip() ' xyz' 

more about rstrip in docs

+104
Mar 03 '10 at 15:37
source share

SilentGhost's answer is pretty neat. The only thing you need to pay attention to is that str.rstrip() returns a copy of the result, this function does not change str .

+6
Oct 28 '16 at 10:35
source share



All Articles