# 练习2 已经一个列表保存多个学生的分数 提取所有学生的姓名 以一个字符串的形式返回
# [{ name : 张三 , age : 18}, { name : 小明 , age : 20}, { name : 小花 , age : 30}] - 张三,小明,小花
students [{ name : 张三 , age : 18}, { name : 小明 , age : 20}, { name : 小花 , age : 30}]
result , .join(x[ name ] for x in students)
print(result)
7. strip、lstrip、rstrip
字符串.strip() - 去掉字符串两端的空白字符 字符串.lstrip() - 去掉字符串前面的空白字符 字符串.rstrip() - 去掉字符串后面的空白字符
str1 tabc 123 n result str1.strip() print( , result, , sep ) result str1.lstrip() print( , result, , sep ) result str1.rstrip() print( , result, , sep )8. replace
字符串1.replace(字符串2 字符串3) - 将字符串1中所有的字符串2替换成字符串3 字符串1.replace(字符串2 字符串3, N) - 替换前N个
str1 how are you? i am fine, thank you! and you? result str1.replace( you , me ) print(result) # how are me? i am fine, thank me! and me? result str1.replace( you , me , 1) print(result) # how are me? i am fine, thank you! and you?9. maketrans, translate
str.maketrans( 字符串1, 字符串2) - 创建一张字符串1和字符串2的字符对应表 字符串.translate(表)
table str.maketrans( ain , 你我他 ) str1 how are you? i am fine, thank you! and you? result str1.translate(table) print(result)
# 练习 将字符串中所有的阿拉伯数字都替换成对应的中文数字 table1 str.maketrans( 0123456789 , 零一二三四五六七八九 ) str2 123木头人 88 result1 str2.translate(table1) print(result1)10. rfind、rindex
str1 how are you? i am fine, thank you! and you? print(str1.find( you )) # 8 print(str1.rfind( you )) # 3911. split
字符串1.split(字符串2) - 将字符串1中所有的字符串2作为切割点对字符串进行切割
注意 如果切割点在最前面或者最后面或者出现连续的切割点 结果会出现空串
str1 abc1abc1abc1bbc1bba1abc1bbc1 result str1.split( 1 ) print(result)



