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

实验2 字符串和列表

时间:2023-03-26 20:44:28浏览次数:29  
标签:%- 10 format 列表 print 实验 10s 字符串 products

试验任务一

task1

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))        #字符串宽度10列,不足左边用0填充
x = 123
print(str(x).zfill(10))   #把int类型转换为字符串类型后,对字符串对象使用str.zfill()方法
print()

x = 'phone_number'
print(x.isidentifier())   #判断字符串是否符合python合法标识符
x = '222test'
print(x.isidentifier())
print()

x = '  '
print(x.isspace())        #判断字符串是否是空白符(包括空格、回车、Tab键等)
x = '\n'
print(x.isspace())
print()

x = 'python is fun'
table = x.maketrans('thon','1234')#为字符串duixiangx创建一个字符映射表,字符thon分别映射到字符1234
print(x.translate(table)) #根据字符映射表table对字符串对象x中的字符进行转换

运行结果

 

 

实验任务二

task2

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 = '-') #指定每个整数宽度占2列;不足2列,左边补0
    i += 1

print('\n整数输出3:',end = '')
i = 0
while i < len(x)-1:
    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)) #x[i]是int类型对象,使用str转化为字符串对象后,处理,然后加入y列表中
    i += 1
print('-'.join(y2))

运行结果

②  当 x = [1,9,8,4,2,0,49] 时,运行结果为

 

实验任务三

task3

name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan','cocteau twins']

#方法一
i = 0
while i < len(name_list):
    print(name_list[i].title())
    i += 1

print()

#方法二
t = []
i = 0
while i < len(name_list):
    t.append(name_list[i].title())
    i += 1

print('\n'.join(t))

运行结果

 

实验任务四

task4

name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan', 'cocteautwins']
x = sorted(name_list)
i = 0
while i < len(x):
    print(f'{i+1}.',x[i].title())
    i += 1

运行结果

 

实验任务五

task5

x = '''The 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!
'''
print('行数:',len(x.splitlines()))
print('单词数:',len(x.split()))
print('字符数;',len(x))
print('空格数:',x.count(' '))

运行结果

 

实验任务六

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

运行结果

实验任务七

data = ['99 81 75','30 42 90 87','69 50 96 77 89 93','82 99 78 100']
list = []
sum = 0
a = 0
for i in data:
    list= i.split(' ')
    for num in list:
        sum += int(num)
        a += 1
print(f'{sum/a:.2f}')

运行结果:

 

实验任务八

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

运行结果:

 

实验任务九

task9_1

#欢迎信息
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.50,33],
['0008','投影仪','松下',1250.00,12],
['0009','吸尘器','飞利浦',999.00,9]
]

#用户输入信息
product_id = input('请输入您要购买的产品编号:')
count = int(input('请输入您要购买的产品数量:'))

#获取编号对应商品的信息
product_index = int(product_id) - 1
product = products[product_index]

#计算金额
print('购买成功,您需要支付',product[3]*count,'元')

#退出系统
print('谢谢您的光临,下次再见!')

运行结果:

task9_2

#欢迎信息
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.50,33],
['0008','投影仪','松下',1250.00,12],
['0009','吸尘器','飞利浦',999.00,9]
]

#产品信息列表
print('产品和价格信息如下:')
print('{:*^55}'.format('*'))
print('{:10}'.format('编号'),'{:10}'.format('名称'),'{:10}'.format('品牌'),'{:10}'.format('价格'),'{:10}'.format('库存数量'))
print('{:-^55}'.format('-'))
print('{:10}'.format(products[0][0]),'{:10}'.format(products[0][1]),'{:10}'.format(products[0][2]),'{:10}'.format(products[0][3]),'{:>10}'.format(products[0][4]))
print('{:10}'.format(products[1][0]),'{:10}'.format(products[1][1]),'{:10}'.format(products[1][2]),'{:10}'.format(products[1][3]),'{:>10}'.format(products[1][4]))
print('{:10}'.format(products[2][0]),'{:10}'.format(products[2][1]),'{:10}'.format(products[2][2]),'{:10}'.format(products[2][3]),'{:>10}'.format(products[2][4]))
print('{:10}'.format(products[3][0]),'{:10}'.format(products[3][1]),'{:10}'.format(products[3][2]),'{:10}'.format(products[3][3]),'{:>10}'.format(products[3][4]))
print('{:10}'.format(products[4][0]),'{:10}'.format(products[4][1]),'{:10}'.format(products[4][2]),'{:10}'.format(products[4][3]),'{:>10}'.format(products[4][4]))
print('{:10}'.format(products[5][0]),'{:10}'.format(products[5][1]),'{:10}'.format(products[5][2]),'{:10}'.format(products[5][3]),'{:>10}'.format(products[5][4]))
print('{:10}'.format(products[6][0]),'{:10}'.format(products[6][1]),'{:10}'.format(products[6][2]),'{:10}'.format(products[6][3]),'{:>10}'.format(products[6][4]))
print('{:10}'.format(products[7][0]),'{:10}'.format(products[7][1]),'{:10}'.format(products[7][2]),'{:10}'.format(products[7][3]),'{:>10}'.format(products[7][4]))
print('{:10}'.format(products[8][0]),'{:10}'.format(products[8][1]),'{:10}'.format(products[8][2]),'{:10}'.format(products[8][3]),'{:>10}'.format(products[8][4]))
print('{:-^55}'.format('-'))

#用户输入信息
product_id = input('请输入您要购买的产品编号:')
count = int(input('请输入您要购买的产品数量:'))

#获取编号对应商品的信息
product_index = int(product_id) - 1
product = products[product_index]

#计算金额
print('购买成功,您需要支付',product[3]*count,'元')

#退出系统
print('谢谢您的光临,下次再见!')

运行结果

 

实验总结:

1.

x.center(10,'*')  
x.ljust(10,'*')  
x.rjust(10,'*') 格式化输出,自动补齐‘*’
x.zfill(10)       不足用0补齐
x.isspace() 判断字符串是否为空白符
为字符串创建一个字符映射表,跟组字符映射表对字符对象x中的字符经行转换

2.

for 循环,while循环

 

 

标签:%-,10,format,列表,print,实验,10s,字符串,products
From: https://www.cnblogs.com/wfg-www/p/17245017.html

相关文章

  • 实验2 目录树的遍历
    Unix实验报告实验:实验2目录树的遍历专业:计算机科学与技术班级:1班姓名:姚怀聿学号:229202022046322022年10月21日目录一......
  • 实验1 同步与异步write的效率比较
    Unix实验报告实验:实验1同步与异步write的效率比较专业:计算机科学与技术班级:1班姓名:姚怀聿学号:229202022046322022年10月7......
  • 实验4 信号处理
    Unix实验报告实验:实验4信号处理专业:计算机科学与技术班级:1班姓名:姚怀聿学号:229202022046322022年12月19日目录一、实......
  • 实验5 哲学家进餐-进程版
    Unix实验报告实验:实验5哲学家进餐-进程版专业:计算机科学与技术班级:1班姓名:姚怀聿学号:229202022046322022年12月2日目录......
  • 实验3 简单shell的设计和实现
    Unix实验报告实验:实验3简单shell的设计和实现专业:计算机科学与技术班级:1班姓名:姚怀聿学号:229202022046322022年11月5日......
  • 实验2 目录树的遍历
    Unix实验报告实验:实验2目录树的遍历专业:计算机科学与技术班级:1班姓名:姚怀聿学号:229202022046322022年10月21日目录一......
  • 访问控制列表
    TCP/IP协议族的传输层协议主要有两个TCP传输控制协议UDP用户数据报协议TCP协议CP是面向连接的、可靠的进程到进程通信的协议TCP提供全双工服务,即数据可在同一时间双向传输......
  • PTA实验1~3分析及总结
    1.前言    自接触面向对象的程序设计这门课以来,已经完成了三次pta上的实验作业了,在这里对前面的练习做一些分析,同时也总结一些经验。    OK,首先,在难度......
  • 03_数字证书实验
    《信息安全综合实践》实验报告数字证书一、实验目的了解密码技术的应用学习OpenSSL的相关命令及应用,了解数字证书的管理了解数字证书的应用二、实验内容序......
  • python基础:split、join、replace、remove、del、pop、index小记python 字符串的split(
    这里总结了平时写脚本时经常用到的一些基础方法,做个记录1、split()函数可以基于分隔符将字符串分割成由若干子串组成的列表str.split(str="",num=string.count(str))str......