author="jolting"
date="2018-03-25"
那天根据项目中自动化测试脚本遇到存储值需要去除空格,找了一下去除中间空格所以搜了一下方法,做了一下笔记。以下所写到的函数中其实均是去除指定字符,不过默认的是去除空格,并且现在是需要去除空格,所以就主要说除去空格喽
1.去除首尾的指定字符 str.strip()

2.去除左边的指定字符 str.lstrip()

3.去除右边的指定字符 str.rstrip()

4.替换字符串中所有需要替换的字符 str.replace(" old", "new")

如果说需要把字符串中的的空格去掉,可以使用replace(" ", "") 。
前四个方法返回的是去除指定字符后的字符串,下面这个方法返回的类型为list
5.切分字符串 str.split()
语法:str.split(str="",num=string.count(str))[n]
参数说明:str:表示为分隔符,默认为空格,但是不能为空('')。若字符串中没有分隔符,则把整个字符串作为列表的一个元素返回
num:表示分割次数。

这几个挺容易理解的,需要的时候希望可以记得用法
Good night
网友评论