Python学习_删除元素的三种方式(del、pop、remove)

str=["你好,旧时光","暗恋 橘生淮南","最好的我们","如果这一秒,没有遇到你""同桌的我","迷雾围城","东宫","平凡的世界","活着"]
print("删除前:",str)
#第一种方式,del,根据下标删除元素
del str[2]
print("用del删除元素:",str)
#第二种方式,pop,删除最后一个元素
str.pop()
print("用pop删除元素",str)
#第三种方式,remove,根据元素移除
str.remove("你好,旧时光")
print("用remove移除元素",str)

运行结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/zyl2726411159/article/details/87936142