通过代码针对mystr = "hello world python and python"进行如下操作
1.找到右边第一个p的索引
mystr = "hello world python and python"
b = mystr.index("p", 22,25)
print(b)
2.找z结果要求报错
mystr = "hello world python and python"
b = mystr.index("z", 1,29)
print(b)
3.将所有空格替换成逗点
def func():
mystr = "hello world python and python"
str2 = mystr.replace(" ",",") #替换符号一致
l = str2.split() #分割
print(l)
func()
4.通过逗点生成一个列表
mystr = "hello world python and python"
print(",".join(str(i) for i in mystr))
5.将d全部替换成大写D
def func():
mystr = "hello world python and python"
str2 = mystr.replace("d","D") #替换符号一致
l = str2.split() #分割
print(l)
func()
a = [1,2,3,4,5,6],在a中元素5前添加一个5.5元素
1.在a中元素5前添加一个5.5元素
a = [1,2,3,4,5,6]
a.insert(4,5.5)
print(a)
2. 在a中元素6后面添加"我心态炸了"
a = [1,2,3,4,5,6]
a.insert(6,"我心态炸了")
print(a)
3. 删除a中的元素1
a = [1,2,3,4,5,6]
a.remove(1)
print(a)
b = [“a”,“b”,“c”,“b”,“a”] 查找索引区间在2-4的“b”的索引
1.查找索引区间在2-4的“b”的索引
a = ["a","b","c","a","b"]
b = a.index("b",2,5)
print(b)
2.查找索引区间在3-4的“a”的索引
a = ["a","b","c","a","b"]
b = a.index("a",1,5)
print(b)
1.找出字典中所有的key
dict = {"name":"单兴伟","id":1,"sex":"男","address":"河北"}
for key in dict.keys():
print(key)
2.找出字典中所有的value
dict = {"name":"单兴伟","id":1,"sex":"男","address":"河北"}
for value in dict.values():
print(value)
3.试着遍历字典中的item
dict = {"name":"单兴伟","id":1,"sex":"男","address":"河北"}
for item in dict.items():
print(item)