设为首页 加入收藏

TOP

第一部分day03-元组、字典、字符串(二)
2019-09-15 00:33:15 】 浏览:79
Tags:第一部分 day03-元组 字典 字符串
查询
1 print(school['teachers']['xiaohu'][0]) #技术好
2 print(school["students"]["zhangsan"][1]) #爱讲笑话
字典嵌套修改
1 school["students"]["zhangsan"][0] = "眼睛很好看"
2 print(school["students"]["zhangsan"][0]) #眼睛很好看
字典排序
1 dic = {6:'666',2:'222',5:'555'}
2 print(sorted(dic)) #[2, 5, 6]
3 print(sorted(dic.values())) #['222', '555', '666']
4 print(sorted(dic.items())) #[(2, '222'), (5, '555'), (6, '666')]
循环遍历
1 dic7 = {'name': 'rise', 'age': 18}
2 for i in dic7:
3     print(("%s:%s") % (i,dic7[i])) #name:rise age:18
 
 
-----字符串-----
 1 a = "this is my progect"
 2 #重复输出字符串
 3 print(a*2) #重复2次输出 this is my progectthis is my progect
 4 #通过索引获取字符串
 5 print(a[3:]) #s is my progect
 6 #in 方法判度
 7 print('is' in a) #True
 8 #格式化输出字符串
 9 print('%s mode1' % a) #this is my progect mode1
10 
11 #字符串拼接
12 a = "this is my progect"
13 b = "test"
14 print("".join([a,b])) #this is my progecttest
15 
16 d = "this is my progect"
17 e = "test"
18 f = ""
19 print(f.join([d,e])) #this is my progecttest
20 
21 #字符串常用内置方法
22 a = "this is my progect"
23 #居中显示
24 print(a.center(50,'*')) #****************this is my progect****************
25 #统计 元素在字符串中重复次数
26 print(a.count("is")) #2
27 #首字母大写
28 print(a.capitalize()) #This is my progect
29 #以某个内容结尾字
30 print(a.endswith("ct")) #True
31 #以某个内容开头字
32 print(a.startswith("th")) #True
33 #调整空格数
34 a = "this\t is my progect"
35 print(a.expandtabs(tabsize=10)) #this       is my progect
36 #查找一个元素,返回元素索引值
37 a = "this is my progect"
38 print(a.find('is')) #2
39 a = "this is my progect{name},{age}"
40 print(a.format(name='dream',age=18)) #this is my progectdream,18
41 print(a.format_map({'name':'rise','age':20})) #this is my progectrise,20
42 print(a.index('s')) #3
43 #判度字符串时候包含数字
44 print("abc1234".isalnum()) #True
45 #检查是否数字
46 print('12345'.isdigit())#True
47 #检查字符串是否合法
48 print('123abc'.isidentifier()) #False
49 print(a.islower()) #True 判断是否全小写
50 print(a.isupper())
51 print('f    d'.isspace()) #是否包含空格
52 print("My Project".istitle()) #首字母大写 True
53 print('my project'.upper()) #MY PROJECT
54 print('my project'.lower()) #my project
55 print('My project'.swapcase()) #mY PROJECT
56 print('my project'.ljust(50,"-")) #my project----------------------------------------
57 print('my project'.rjust(50,'-')) #----------------------------------------my project
58 #去掉字符串空格与换行符
59 print("     my project\n".strip()) #my project
60 print('test')
61 #替换
62 print("my project project".replace('pro','test',1)) #my testject project
63 #从右向左查找
64 print("my project project".rfind('t')) #17
65 #以右为准分开
66 print("my project project".rsplit('j',1)) #['my project pro', 'ect']
67 print("my project project".title()) #My Project Project
首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇小白专场-树的同构-python语言实现 下一篇虚拟环境的搭建

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目