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

实验二 字符串和列表

时间:2023-03-28 13:15:14浏览次数:33  
标签:%- 10 list len 列表 print 实验 字符串 10s

任务一:

x = 'nba FIFA'
print(x.upper())
print(x.lower())
print(x.swapcase())
print()
x = 'abc'
print(x.center(10, '*'))
print(x.ljust(10, '*'))
print(x.rjust(10, '*'))
print()
x = '123'
print(x.zfill(10))
x = 123
print(str(x).zfill(10))
print()
x = 'phone_number'
print(x.isidentifier())
x = '222test'
print(x.isidentifier())
print()
x = ' '
print(x.isspace())
x = '\n'
print(x.isspace())
print()
x = 'python is fun'
table = x.maketrans('thon', '1234')
print(x.translate(table))

运行结果:

任务二:

x = [5, 11, 9, 7, 42]
print('整数输出1: ', end = '')
i = 0
while i < len(x):
    print(x[i], end = ' ')
    i += 1
print('\n整数输出2: ', end = '')
i = 0
while i < len(x):
    print(f'{x[i]:02d}', end = '-')
    i += 1
print('\n整数输出3: ', end = '')
i = 0
while i < len(x):
    print(f'{x[i]:02d}', end = '-')
    i+=1
print(f'{x[-1]:02d}')

print('\n字符输出1: ', end = '')
y1 = []
i = 0
while i < len(x):
    y1.append(str(x[i]))
    i += 1
print('-'.join(y1))
print('字符输出2: ', end = '')
y2 = []
i = 0
while i < len(x):
    y2.append( str(x[i]).zfill(2) )
    i += 1
print('-'.join(y2))

 

运行结果:

任务三:

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

 

运行结果:

任务四:

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

运行结果:

任务五:

s="""
Zen Of Python , by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
"""
hang=s.splitlines()
print("行数:",len(hang))
ci=s.split()
print("单词数:",len(ci))
zi=str(s)
print("字符数:",len(zi.capitalize()))
kong=s.count(" ")
print("空格数:",kong)

运行结果:

任务六:

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

运行结果:

任务七:

1 data = ['99 81 75', '30 42 90 87', '69 50 96 77 89 93', '82 99 78 100']
2 sum=0
3 time=0
4 for i in data:
5     for ii in i.split():
6         sum+=int(ii)
7         time+=1
8 average=sum/time
9 print(f"平均数为{average:.2f}")

运行结果:

任务八:

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

运行结果:

任务九:

print('欢迎使用家用电器销售系统!')
#产品信息列表
print('产品和价格信息如下:')
print ( '************************************')
print('%-10s'%'编号','%-10s'%'名称','%-10s'%'品牌','%-10s'%'价格','%-10s'%'库存数量')
print('  - - ' )
print('%-10s'%'0001','%-10s'%'电视机','%-10s'%'海尔','%10.2f'%5999.00,
'%10d ' %20)
print('%-10s'%'0002','%-10s'%'冰箱','%-10s'%'西门子','%10.2f'%6998.00,
'%10d ' %15)
print('%-10s'%'0003','%-10s'%'洗衣机','%-10s'%'小天鹅','%10.2f'%1999.00,
'%10d ' %10)
print('%-10s'%'0004','%-10s'%'空调','%-10s'%'格力','%10.2f'%3900.00,
'%10d ' %0)
print('%-10s'%'0005','%-10s'%'热水器','%-10s'%'美的','%10.2f'%688.00,
'%10d ' %30)
print('%-10s'%'0006','%-10s'%'笔记本','%-10s'%'联想','%10.2f'%5699.00,
'%10d ' %10)
print('%-10s'%'0007','%-10s'%'微波炉','%-10s'%'苏泊尔','%10.2f'%480.50,
'%10d' %33)
print('%-10s'%'0008','%-10s'%'投影仪','%-10s'%'松下','%10.2f'%1250.00,
'%10d ' %12)
print('%-10s'%'0009','%-10s'%'吸尘器','%-10s'%'飞利浦','%10.2f'%999.00,'%10d ' %9)
print(' ' )
#商品数据
products=[
  ['0001','电视机','海尔',5999.00,20],
  ['0002','冰箱','西门子',6998.00,15],
  ['0003','洗衣机','小天鹅',1999.00,10],
  ['0004','空调','格力',3900.00,0],
  ['0005','热水器','格力',688.00,30],
  ['0006','笔记本','联想',5699.00,10],
  ['0007','微波炉','苏泊尔',480.00,33],
  ['0008','投影仪','松下',1250.00,12],
  ['0009','吸尘器','飞利浦',999.00,9],
]
#用户输入信息
product_id= input('请输入您要购买的产品编号:')
count=int(input('请输入您要购买的产品数量:'))
#获取编号对应商品的信息
product_index=len(product_id)-1
product=products[product_index]
#计算金额
print(f'购买成功,您需要支付{product[3]*count}元')
#退出系统
print('谢谢您的光临,下次再见!')

运行结果:

 

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

相关文章

  • 字符串内置的函数
      a='studesnt'print(a.capitalize())print(a.count('s'))#字符串的位置查找返回的下标中,字符串的第一个字符的下标定义为0.print(a.find('n'))print(a.find('o'))p......
  • 实验一 密码引擎-2-电子钥匙功能测试
    一、任务详情0参考附件中的视频1解压"资源"中“龙脉密码钥匙驱动实例工具等”压缩包2在Ubuntu中运行“龙脉密码钥匙驱动实例工具等\mToken-GM3000\skf\samples\linux......
  • Element-UI el-upload组件,上传失败,但是依然显示文件列表
    问题描述最近在使用element-ui的el-upload组件,发现一个问题,就是我在上传文件过程中,上传失败了,文件列表仍然展示该文件。解决办法上传成功on-success回调方法中//除......
  • C# Linq获取List列表中某个字段最大值对应的记录
    就以下面的列表举个小例子吧:List<T>epList=newList<T>();方法1:试了Max()取最大值的方法,但是方法返回的是一个特定的值,而不是对应的一条记录;有些不方便,当然......
  • JSON对象与JSON字符串
    一、JSON对象有时候在做项目的时候时常将这两个概念弄混淆,尤其是在使用springmvc的时候,后台@RequestBody接受的是一个json格式的字符串,一定是一个字符串。先介绍一下js......
  • (原创)【B4A】一步一步入门09:xCustomListView,加强版列表、双行带图片、复选框按钮等自定
    一、前言上篇((原创)【B4A】一步一步入门08:ListView,列表、单行、双行、双行带图片、列表项样式(控件篇04))我们讲了ListView,目前官方已经不推荐再使用ListView了,而是推荐使用xCu......
  • 练习02_列表元组
    快速回顾前一次内容,我们学习了以下内容:创建并登陆了百度飞桨Paddle&PaddleBaiduAIStudio个人账号,该云计算代码测试与托管平台是免费的,24小时访问的。我们测试了“hell......
  • 字符串的常用操作
    这是字符串的常用操作汇总  下面是字符串的特点......
  • linux sed 批量替换文件中的字符串或符号
    sed-i:直接修改读取的文件内容,而不是输出到终端。 sed-i 就是直接对文本文件进行操作的 替换每行第一次出现的字符串sed-i's/查找的字符串/替换的字符串/'文......
  • 实验一 密码引擎-2-电子钥匙功能测试
    实验一密码引擎-2-电子钥匙功能测试1解压“龙脉密码钥匙驱动实例工具等”压缩包2在Ubuntu中运行“龙脉密码钥匙驱动实例工具等\mToken-GM3000\skf\samples\linux_mac......