安岳网站建设设计

将想法与焦点和您一起共享

创新互联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.scanyue.cn/article/dhsiggo.html

其他资讯