成都创新互联网站制作重庆分公司

创新互联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://www.cxhlcq.com/article/dhsiggo.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部