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视频教程栏目。
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 找不到素材资源介绍文章里的示例图片?
- 模板不会安装或需要功能定制以及二次开发?
发表评论
还没有评论,快来抢沙发吧!