十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
python中删除字符串中空格的方法:

专注于为中小企业提供成都做网站、成都网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业定边免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
1、使用replace()函数,把所有的空格(" ")替换为("")
def remove(string):
return string.replace(" ", "");
string = ' H E L L O ! ';
print("原字符串:"+string) ;
print("\n新字符串:"+remove(string)) ;输出结果:
原字符串: H E L L O !
新字符串:HELLO!
2、使用lstrip()函数删除字符串左边空格
lstrip()方法语法:
str.lstrip([chars])
示例:
#!/usr/bin/python
str = " this is string example....wow!!! ";
print str.lstrip();
str = "88888888this is string example....wow!!!8888888";
print str.lstrip('8');输出结果:
this is string example....wow!!!
this is string example....wow!!!8888888
3、使用rstrip()函数删除字符串末尾空格
rstrip()方法语法:
str.rstrip([chars])
示例:
#!/usr/bin/python
str = " this is string example....wow!!! ";
print str.rstrip();
str = "88888888this is string example....wow!!!8888888";
print str.rstrip('8');输出结果如下:
this is string example....wow!!!
88888888this is string example....wow!!!
网站名称:创新互联Python教程:python中如何删除字符串中的空格
文章起源:http://www.zsjierui.cn/article/cocddho.html