>>a.lstr" />

Python strip() 函数 去空格 \n \r \t 函数的用法

您所在的位置:网站首页 python字符串去掉两边空格 Python strip() 函数 去空格 \n \r \t 函数的用法

Python strip() 函数 去空格 \n \r \t 函数的用法

2023-08-08 09:27| 来源: 网络整理| 查看: 265

在 Python 中字符串处理函数里有三个去空格(包括 ‘\n’, ‘\r’, ‘\t’, ’ ') 的函数:

strip 同时去掉左右两边的空格lstrip 去掉左边的空格rstrip 去掉右边的空格

具体示例如下:

>>> a=" gho stwwl " >>> a.lstrip() 'gho stwwl ' >>> a.rstrip() ' gho stwwl' >>> a.strip() 'gho stwwl'

声明:

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

注意:

当 rm 为空时,默认删除空白符(包括 ‘\n’, ‘\r’, ‘\t’, ’ ') >>> a = ' 123' >>> a.strip() >>> '123' >>> a='\t\tabc' >>> 'abc' >>> a = 'sdff\r\n' >>> a.strip() >>> 'sdff' 这里的 rm 删除序列是只要边(开头或结尾)上的字符在删除序列内,就删除掉。 >>> a = '123abc' >>> a.strip('21') >>> '3abc' 结果是一样的 >>> a.strip('12') >>> '3abc'

via:https://www.cnblogs.com/zdz8207/p/python_learn_note_20.html



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3