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

疆括仕网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

创新互联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如何去空格和回车?
本文路径:https://tyhkzb.com/article/dhsiggo.html

在线咨询
服务热线
服务热线:028-86922220
TOP