首页 > 其他分享 >实验2 字符串和列表

实验2 字符串和列表

时间:2023-03-23 22:13:52浏览次数:47  
标签:%- 10 list len 列表 print 实验 字符串 10s

1.实验任务1

task1.py

 1 #字符串的基础操作
 2 
 3 x = 'nba FIFA'
 4 print(x.upper())    #字符串转大写
 5 print(x.lower())    #字符串转小写
 6 print(x.swapcase()) #字符串大小写翻转
 7 print()
 8 
 9 x = 'abc'
10 print(x.center(10, '*'))   #字符串居中,宽度10列,不足左右补*号
11 print(x.ljust(10, '*'))    #字符串居左,宽度10列,不足右边补*号
12 print(x.rjust(10, '*'))    #字符串居右,宽度10列,不足左边补*号
13 print()
14 
15 x = '123'
16 print(x.zfill(10))        #字符串宽度10列,不足左边用0填充
17 x = 123
18 print(str(x).zfill(10))    #把int类型转换成字符串类型后,对字符串对象使用str.fill()方法
19 print()
20 
21 x = 'phone_number'
22 print(x.isidentifier())    #判断字符串是否是python合法标识符
23 x = '222test'
24 print(x.isidentifier())
25 print()
26 
27 
28 x = '  '
29 print(x.isspace())      #判断字符串是否是空白符(包括空格、回车、Tab键)
30 x = '\n'
31 print(x.isspace())
32 print()
33 
34 
35 x = 'python is fun'
36 table = x.maketrans('thon','1234')    #为字符串对象x创建一个字符映射表,字符thon分别映射到字符1234
37 print(x.translate(table))  #根据字符映射表table对字符串对象x中的字符串进行转换

2.实验任务2

task2.py

 1 #基础练习:列表、格式化、类型转换
 2 
 3 x = [5,11,9,7,42]
 4 
 5 print('整数输出1:',end = '')
 6 i = 0
 7 while i < len(x):
 8     print(x[i],end = ' ')
 9     i+=1
10 
11 
12 
13 print('\n整数输出2:', end = '')
14 i=0
15 while i < len(x):
16     print(f'{x[i]:02d}',end = '-')#指定每个整数宽度占2列:不足2列,左边补p0
17     i+=1
18 
19 
20 print('\n整数输出3:', end = '')
21 i = 0
22 while i < len(x)-1:
23     print(f'{x[i]:02d}' , end = '-')
24     i += 1
25 print(f'{x[-1]:02d}')
26 
27 
28 print('\n字符输出1:', end = '')
29 y1 = []
30 i = 0
31 while i < len(x):
32     y1.append(str(x[i]))  #函数str()用于把其他类型对象转换成字符串对象
33     i += 1
34 print('-'.join(y1))
35 
36 
37 print('字符输出2:', end = '')
38 y2 = []
39 i = 0
40 while i < len(x):
41     y2.append(str(x[i]).zfill(2)) #x[i]是int类型对象,使用str()转换成字符串对象后处理,然后加入到列表中
42     i += 1
43 print('-'.join(y2))

3.实验任务3

task3.py

 1 #把姓名转换成大写,遍历分行输出
 2 
 3 name_list = ['david bowie','louis armstrong','leonard cohen','bob dylan','cocteau twins']
 4 
 5 #方法1
 6 i = 0
 7 while i < len(name_list):
 8     print(name_list[i].title())
 9     i += 1
10 
11 print()
12 
13 #方法2
14 t = []
15 i = 0
16 while i < len(name_list):
17     t.append(name_list[i].title())
18     i += 1
19 
20 print('\n'.join(t))

4.实验任务4

task4.py

1 name_list = ['david bowie','louis armstrong','leonard cohen','bob dylan','cocteau twins']
2 
3 name_list1 = sorted(name_list)
4 i = 0
5 while i < len(name_list1):
6     print(f'{i+1}. {name_list1[i].title()}')
7     i += 1

5.实验任务5

task5.py

 

 1 text='''The Zen of Python, by Tim Peters
 2 
 3 Beautiful is better than ugly.
 4 Explicit is better than implicit.
 5 Simple is better than complex.
 6 Complex is better than complicated.
 7 Flat is better than nested.
 8 Sparse is better than dense.
 9 Readability counts.
10 Special cases aren't special enough to break the rules.
11 Although practicality beats purity.
12 Errors should never pass silently.
13 Unless explicitly silenced.
14 In the face of ambiguity, refuse the temptation to guess.
15 There should be one-- and preferably only one --obvious way to do it.
16 Although that way may not be obvious at first unless you're Dutch.
17 Now is better than never.
18 Although never is often better than *right* now.
19 If the implementation is hard to explain, it's a bad idea.
20 If the implementation is easy to explain, it may be a good idea.
21 Namespaces are one honking great idea -- let's do more of those!'''
22 a = len(text.split('\n'))
23 b = len(text.split())
24 c = len(text)
25 d = text.count(' ')
26 print(f'行数:{a}')
27 print(f'单词数:{b}')
28 print(f'字符数:{c}')
29 print(f'空格数:{d}')

6.实验任务6

task6.py

 1 book_list=[['静静的顿河','肖洛霍夫','金人','人民文学出版社'],
 2            ['大地之上','罗欣顿.米斯特里','张亦琦','天地文学出版社'],
 3            ['夜航西飞','柏瑞尔.马卡姆','陶立夏','人民文学出版社'],
 4            ['来自民间的叛逆','袁越','','新星出版社'],
 5            ['科技与恶的距离','珍妮.克里曼','詹蕎語','墨刻出版社'],
 6            ['灯塔','克里斯多夫.夏布特','吕俊君','北京联合出版公司'],
 7            ['小行星掉在下午','沈大成','','广西师范大学出版社']]
 8 
 9 print(f'{"图书信息":*^60}')
10 i=0
11 while i < len(book_list):
12     x=book_list[i]
13     print(i+1 , end ='.')
14     print('|'.join(x))
15     i+=1

7.实验任务7

task7.py

 1 data = ['99 81 75', '30 42 90 87', '69 50 96 77 89 93', '82 99 78 100']
 2 i=0
 3 n=[]
 4 while i < len(data):
 5     x=data[i].split()
 6     n+=x
 7     i+=1
 8 s=0
 9 t=0
10 while t < len(n):
11     s+=eval(n[t])
12     t+=1
13 print(f'{s/len(n):.2f}')

8.实验任务8

task8.py

 1 words_sensitive_list = ['张三', 'V字仇杀队', '杀']
 2 comments_list = ['张三因生命受到威胁正当防卫导致过失杀人,经辩护律师努力,张三不需负刑事责任。','电影<V字仇杀队>从豆瓣下架了','娱乐至死']
 3 str2=''
 4 list2=[]
 5 for list1 in words_sensitive_list:
 6     list2+=list(list1)
 7 for str1 in comments_list:
 8     str2+=str1
 9 i=0
10 while i < len(list2):
11     str2=str2.replace(list2[i],'*')
12     i+=1
13 print(str2)

9.实验任务9

task9_1.py

 

 1 print('欢迎使用家用电器销售系统!')
 2 
 3 print('产品和价格信息如下:')
 4 print('*'*60)
 5 
 6 print('产品和价格信息如下:')
 7 print('*'*60)
 8 print('%-10s' %'编号','%-10s' %'名称','%-10s' %'品牌', '%-10s' %'价格', '%-10s' %'库存数量')
 9 print('-'*60)
10 print('%-10s' %'0001','%-10s' %'电视机','%-10s' %'海尔', '%10.2f' %5999.00, '%-10d' %20)
11 print('%-10s' %'0002','%-10s' %'冰箱','%-10s' %'西门子', '%10.2f' %6998.00, '%-10d' %15)
12 print('%-10s' %'0003','%-10s' %'洗衣机','%-10s' %'小天鹅', '%10.2f' %1999.00, '%-10d' %1)
13 print('%-10s' %'0004','%-10s' %'空调','%-10s' %'格力', '%10.2f' %3900.00, '%-10d' %0)
14 print('%-10s' %'0005','%-10s' %'热水器','%-10s' %'美的', '%10.2f' %688.00, '%-10d' %30)
15 print('%-10s' %'0006','%-10s' %'笔记本','%-10s' %'联想', '%10.2f' %5699.00, '%-10d' %20)
16 print('%-10s' %'0007','%-10s' %'微波炉','%-10s' %'苏泊尔', '%10.2f' %480.50, '%-10d' %33)
17 print('%-10s' %'0008','%-10s' %'投影仪','%-10s' %'松下', '%10.2f' %1250.00, '%-10d' %12)
18 print('%-10s' %'0009','%-10s' %'吸尘器','%-10s' %'飞利浦', '%10.2f' %999.00, '%-10d' %9)
19 print('-'*60)
20 
21 products=[
22     ['0001','电视机','海尔',5999.00,20],
23     ['0002','冰箱','西门子',6998.00,15],
24     ['0003','洗衣机','小天鹅',1999.00,10],
25     ['0004','空调','格力',3900.00,0],
26     ['0005','热水器','格力',688.00,30],
27     ['0006','笔记本','联想',5699.00,10],
28     ['0007','微波炉',' 苏泊尔',480.00,33],
29     ['0008','投影仪','松下',1250.00,12],
30     ['0009','吸尘器','飞利浦',999.00,9],
31     ]
32 product_id=input('请输入您要购买的产品编号:')
33 count=int(input('请输入您要购买的产品数量:'))
34 
35 product_index=len(product_id)-1
36 product=products[product_index]
37 
38 print('购买成功,您需要支付',product[3]*count,'元')
39 
40 print('谢谢您的光临,下次再见!')

task9_2.py

 

 1 print('欢迎使用家用电器销售系统!')
 2 
 3 print('产品和价格信息如下:')
 4 print('*'*60)
 5 
 6 print('产品和价格信息如下:')
 7 print('*'*60)
 8 print(f'{"编号":<10} {"名称":<10} {"品牌":<10} {"价格":<10} {"库存数量":<10}')
 9 print('-'*60)
10 print(f'{"0001":<10} {"电视机":<10} {"海尔":<10} {5999:.2f} {20:10}')
11 print(f'{"0002":<10} {"冰箱":<10} {"西门子":<10} {6998:.2f} {15:10}')
12 print(f'{"0003":<10} {"洗衣机":<10} {"小天鹅":<10} {1999:.2f} {10:10}')
13 print(f'{"0004":<10} {"空调":<10} {"格力":<10} {3900:.2f} {0:10}')
14 print(f'{"0005":<10} {"热水器":<10} {"美的":<10} {688:.2f} {30:10}')
15 print(f'{"0006":<10} {"笔记本":<10} {"联想":<10} {5699:.2f} {10:10}')
16 print(f'{"0007":<10} {"微波炉":<10} {"苏泊尔":<10} {480.5:.2f} {33:10}')
17 print(f'{"0008":<10} {"投影仪":<10} {"松下":<10} {1250:.2f} {12:10}')
18 print(f'{"0009":<10} {"吸尘器":<10} {"飞利浦":<10} {999:.2f} {9:10}')
19 print('-'*60)
20 products=[
21     ['0001','电视机','海尔',5999.00,20],
22     ['0002','冰箱','西门子',6998.00,15],
23     ['0003','洗衣机','小天鹅',1999.00,10],
24     ['0004','空调','格力',3900.00,0],
25     ['0005','热水器','格力',688.00,30],
26     ['0006','笔记本','联想',5699.00,10],
27     ['0007','微波炉',' 苏泊尔',480.00,33],
28     ['0008','投影仪','松下',1250.00,12],
29     ['0009','吸尘器','飞利浦',999.00,9],
30     ]
31 product_id=input('请输入您要购买的产品编号:')
32 count=int(input('请输入您要购买的产品数量:'))
33 
34 product_index=len(product_id)-1
35 product=products[product_index]
36 
37 print('购买成功,您需要支付',product[3]*count,'元')
38 
39 print('谢谢您的光临,下次再见!')

 

标签:%-,10,list,len,列表,print,实验,字符串,10s
From: https://www.cnblogs.com/wang2003chengxu/p/17243213.html

相关文章