Python怎么去掉最后的空格

Python怎么去掉最后的空格,第1张

Python怎么去掉最后的空格 strip()函数 去空格\n\r\t函数的用法

strip 同时去掉左右两边的空格(推荐学习:Python视频教程)

lstrip 去掉左边的空格

rstrip 去掉右边的空格

具体示例如下:

>>>a=" hello world!!  "
>>>a.lstrip() 'hello world!! '
>>>a.rstrip() ' hello world!!'
>>>a.strip() 'hello world!!'

声明:s为字符串,rm为要删除的字符序列

s.strip(rm) 删除s字符串中开头、结尾处,位于 rm删除序列的字符

s.lstrip(rm) 删除s字符串中开头处,位于 rm删除序列的字符

s.rstrip(rm) 删除s字符串中结尾处,位于 rm删除序列的字符

注意:

当rm为空时,默认删除空白符(包括’\n’, ‘\r’, ‘\t’, ’ ')

>>> a = ' hello world!!'
>>> a.strip()
'hello world!!'
>>> a='\t\thello world!!'
'hello world!!'
>>> a = 'hello world!!\r\n'
>>> a.strip()
'hello world!!'

更多Python相关技术文章,请访问Python教程栏目进行学习!

以上就是Python怎么去掉最后的空格的详细内容,

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/681192.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-20
下一篇 2022-04-20

发表评论

登录后才能评论

评论列表(0条)

保存