网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

python中删除字符串中空格的方法-创新互联

创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!

嘉善网站制作公司哪家好,找创新互联!从网页设计、网站建设、微信开发、APP开发、自适应网站建设等网站项目制作,到程序开发,运营维护。创新互联自2013年创立以来到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联。

这篇文章将为大家详细讲解有关python中删除字符串中空格的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

python中删除字符串中空格的方法:

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://bjjierui.cn/article/deseie.html

其他资讯