Python 处理删除和替换 remove() pop() replace()

1、remove() (处理列表的函数)

remove() 这个函数是根据列表的内容的删除函数,例子如下所是:

aList = [123, 'a', 'b', 'c', 'd'];

aList.remove('a');
print "List : ", aList;

输出结果为:

List :  [123, 'b', 'c', 'd']

2、pop() (处理列表的函数)

pop() 这个函数是根据列表的位置函数,例子如下所是:

aList = [123, 'a', 'b', 'c', 'd'];

aList.pop(0);   # 0是序列号
print "List : ", aList;

输出结果为:

List :  ['a', 'b', 'c', 'd']

3、replace() (处理字符串的函数)

这个函数可以替换字符换的有关内容
replace(a, b) 将字符串中的a换成b

str = "this is string";
print str.replace("is", "was");

输出结果为:

thwas was string

补充假设字符串str

str.replace(‘ ’, ‘’) #删除字符串中的空格
str.replace(‘ ’, ‘’, 3) #删除字符串中的空格, 最多删除3个
发布了97 篇原创文章 · 获赞 221 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_32642107/article/details/104519172