首页 > 其他分享 >实验二——字符串,列表

实验二——字符串,列表

时间:2023-03-27 22:33:50浏览次数:40  
标签:%- information 10 list 列表 print 实验 字符串 10s

任务一

程序:

 1 x = 'nba FIFA'
 2 print(x.upper())
 3 print(x.lower())
 4 print(x.swapcase())    #大小写翻转
 5 
 6 x = 'abc'
 7 print(x.center(10,'*'))
 8 print(x.ljust(10,'*'))
 9 print(x.rjust(10,'*'))
10 
11 x = '123'
12 print(x.zfill(10))
13 x = 123
14 print(str(x).zfill(10))
15 print()
16 
17 x = 'phone_number'
18 print(x.isidentifier())  #判断是否为python的合法字符
19 x = '222test'
20 print(x.isidentifier())
21 print()
22 
23 x = ' '
24 print(x.isspace())   #判断是否为空白符(包括空格,回车,tab键)
25 x = '\n'
26 print(x.isspace())
27 print()
28 
29 x = 'python is fun'
30 table = x.maketrans('thon', '1234')  #为字符串对象x创建一个字符映射表,字符thon分别映射字符的1234
31 print(x.translate(table))

结果:

 

任务二

程序:

 1 x = [1,9,8,4,2,0,49]
 2 
 3 print('整数输出1:',end = ' ')
 4 i = 0
 5 while i < len(x):
 6     print(x[i],end = ' ')
 7     i += 1
 8 
 9 
10 
11 print('\n整数输出2:', end = ' ')
12 i = 0
13 while i <len(x):
14     print(f'{x[i]:02d}', end = '-')
15     i += 1
16 
17 
18 print('\n整数输出3:', end = '')
19 i = 1
20 while i <len(x) - 1:
21     print(f'{x[i]:02d}',end = '-')
22     i += 1
23 print(f'{x[-1]:02d}', end = '-')
24 
25 print('\n')
26 print('\n字符输出1:', end = '')
27 y1 = []
28 i = 0
29 while i < len(x):
30     y1.append(str(x[i]))
31     i += 1
32 print('-'.join(y1))
33 
34 print('字符输出2:', end = '')
35 y2 = []
36 i = 0
37 while i < len(x):
38     y2.append(str(x[i]).zfill(2))
39     i += 1
40 print('-'.join(y2))

结果:

 

任务三

程序:

 1 name_list = ['david bowie','louis armstrong','leonard cohen','bob dylan','cocteau twins']
 2 
 3 i = 0
 4 while i < len(name_list):
 5     print(name_list[i].title())
 6     i += 1
 7 
 8 print()
 9 
10 
11 t = []
12 i = 0
13 while i <len(name_list):
14     t.append(name_list[i].title())
15     i += 1
16 
17 print('\n'.join(t))

结果:

 

任务四

程序:

 1 name_list = ['david bowie','louis armstrong','leonard cohen','bob dylan','cocteau twins']
 2 name_list.sort()
 3 
 4 t = []
 5 i = 0
 6 while i < len(name_list):
 7     t.append(name_list[i].title())
 8     i += 1
 9 x = 0
10 while x < len(name_list):
11     print(f'{x+1}.'+ t[x])
12     x += 1

结果:

 

任务五

程序:

 1 s = '''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 '''
23 a = len(s.splitlines())
24 b = len(s)
25 c = len(s.split())
26 d = s.count(' ')
27 print(f'行数:{a}')
28 print(f'字符数:{b}')
29 print(f'字符数:{c}')
30 print(f'空格数:{d}')

结果:

 

任务六

程序:

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

结果:

 

任务七

程序:

 1 '''
 2 某.csv格式数据文件内数据如下:
 3 99 81 75
 4 30 42 90 87
 5 69 50 96 77 89, 93
 6 82, 99, 78, 100
 7 '''
 8 data = ['99 81 75', '30 42 90 87', '69 50 96 77 89 93', '82 99 78 100']
 9 data_str = ','.join(data)   #转换为字符串
10 data_str1 = data_str.replace(',',' ')    #转化为都是逗号隔开的字符串
11 n = data_str1.split()       #转化为list类型
12 i = 0
13 sum = 0
14 while i < len(n):
15     sum += int(n[i])
16     i += 1
17 average = sum/len(n)
18 print(f'{average:.2f}')

结果:

 

任务八

程序:

1 words_sensitive_list = ['张三', 'V字仇杀队', '杀']
2 comments_list = ['张三因生命受到威胁正当防卫导致过失杀人,经辩护律师努力,张三不需负刑事责任。',
3 '电影<V字仇杀队>从豆瓣下架了',
4 '娱乐至死']
5 comments_str = ','.join(comments_list)
6 comments_str1 = comments_str.replace('张三','**').replace('V字仇杀队','*****').replace('杀','*')
7 comments_str2 = comments_str1.replace(',','\n')
8 print(comments_str2)

结果:

 

任务九

task9_1:

程序:

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

结果:

task9_2:

程序:

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

结果:

 

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

相关文章

  • 实验一 密码引擎-2-电子钥匙功能测试
    目录1解压"资源"中“龙脉密码钥匙驱动实例工具等”压缩包2在Ubuntu中运行“龙脉密码钥匙驱动实例工具等\mToken-GM3000\skf\samples\linux_mac”中例程,提交运行结果截图......
  • Spinner(列表选项框)的基本使用
    这一节是想给大家介绍一个Gallery(画廊)的一个控件,尽管我们可以不通过兼容使用Gallery,不过想想还是算了,因为Gallery在每次切换图片的时候,都需要重新创建视图,这样无疑会造成......
  • 实验一 密码引擎-2-电子钥匙功能测试
    在Ubuntu中运行“龙脉密码钥匙驱动实例工具等\mToken-GM3000\skf\samples\linux_mac”中例程,提交运行结果截图加分项:运行“龙脉密码钥匙驱动实例工具等\mToken-GM3000......
  • dom4j 解析xml string 字符串
    packagedom4j;importjava.util.Iterator;importorg.dom4j.Document;importorg.dom4j.DocumentException;importorg.dom4j.DocumentHelper;importorg.dom4j.......
  • 实验一 密码引擎-2-电子钥匙功能测试
    任务详情0参考附件中的视频1解压"资源"中“龙脉密码钥匙驱动实例工具等”压缩包2在Ubuntu中运行“龙脉密码钥匙驱动实例工具等\mToken-GM3000\skf\samples\linux_ma......
  • 202031604107-米乐文 实验一 软件工程准备—初识软件工程
    项目内容课程班级博客链接2020级卓越工程师班这个作业要求链接实验一——软件工程准备我的课程学习目标1.学会使用博客园进行学习2.了解Github工具的......
  • 实验二 字符串和列表
    task1源代码#字符串基本操作x='nbaFIFA'print(x.upper())#转大写print(x.lower())#转小写print(x.swapcase())#大小写翻转print()x='abc'print(......
  • 实验二
    1x='nbaFIFA'2print(x.upper())3print(x.lower())4print(x.swapcase())5print()67x='abc'8print(x.center(10,'*'))9print(x.ljust(10,'*......
  • 实验2
    task1.pyx='nbaFIFA'print(x.upper())print(x.lower())print(x.swapcase())print()x='abc'print(x.center(10,'*'))print(x.ljust(10,'*'))print(x.rjust(10,'......
  • 1047. 删除字符串中的所有相邻重复项
    给出由小写字母组成的字符串S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。在S上反复执行重复项删除操作,直到无法继续删除。在完成所有重复项删除操作后返回......