创新互联Python教程:python如何去空格和回车?

python去掉空格和回车的方法:

1、使用strip()、lstrip()、rstrip()等方法删除字符串中的空格(这些方法只能删除字符串两端的空格)

 "   xyz   ".strip()            # returns "xyz"
 "   xyz   ".lstrip()           # returns "xyz   "
 "   xyz   ".rstrip()           # returns "   xyz"

2、使用replace()方法将空格替换为空

"  x y z  ".replace(' ', '') # returns "xyz"

3、使用replace("\n", "")方法将换行符替换为空。


本文名称:创新互联Python教程:python如何去空格和回车?
标题来源:http://jxruijie.cn/article/dhsiggo.html