如何在Python中从字符串末尾删除空格?


91

我需要在字符串中的单词之后删除空格。可以用一行代码完成吗?

例:

string = "    xyz     "

desired result : "    xyz" 

3
这个问题真的需要在这里问吗?在docs中很容易找到:docs.python.org/library/stdtypes.html#str.rstrip
Greg K

11
@Greg K是的,因为即使是那些阅读文档的人也可能没有意识到它可能在那里,因为将阅读的前几遍都调校出来并记住它说的是无关紧要的内容是很基本的。此外,文档中的rstrip不会在Google搜索中轻松显示此问题(使用条件“ python strip string of string”)。
Brōtsyorfuzthrāx

4
真实感冒@GregK真实感冒
deepelement 2015年

Answers:


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.