栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python第一阶段学习总结

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python第一阶段学习总结

# 练习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 )) # 39
11. split
字符串1.split(字符串2) - 将字符串1中所有的字符串2作为切割点对字符串进行切割 

注意 如果切割点在最前面或者最后面或者出现连续的切割点 结果会出现空串

str1 abc1abc1abc1bbc1bba1abc1bbc1 
result str1.split( 1 )
print(result)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/267760.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号