一、实验任务
1.实验任务1
程序源代码:
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.zfill(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))
2.实验任务2
程序源代码
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) - 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)) i += 1 print('-'.join(y2))
3.实验任务3
#把姓名转换成大写 #方法1 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() #方法2 t = [] i = 0 while i < len(name_list): t.append(name_list[i].title()) i += 1 print('\n'.join(t))
4.实验任务4
程序源代码
name_list1 = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan', 'cocteau twins'] name_list1.sort()#按字母排序 num = 0 for i in name_list1: num += 1 print(num, end = '.') print(i.title()) if num >=len(name_list1):#如果大于人头数就停止 break
5.实验任务
程序源代码
import this #导入 Zen of Python到字符串中 str1 = "".join([this.d. get(c,c) for c in this.s]) print("行数:",str1.count('\n')) print("单词数:",len(str1.split())) print("字符数:",len(str1)) print("空格数:",str1.count(' '))
6.实验任务
程序源代码
book_list = [['静静的顿河','肖洛霍夫','金人', '人民文学出版社'], ['大地之上','罗欣顿.米斯特里','张亦琦', '天地出版社'], ['夜航西飞', '柏瑞尔.马卡姆', '陶立夏', '人民文学出版社'], ['来自民间的叛逆', '袁越', '','新星出版社'], ['科技与恶的距离', '珍妮.克里曼', ' 詹蕎語', '墨刻出版社'], ['灯塔','克里斯多夫.夏布特','吕俊君','北京联合出版公司'], ['小行星掉在下午','沈大成', '', '广西师范大学出版社']] t = '图书信息' print(t.center(40,"*")) for t in range(0,len(book_list)): print(t+1,end='.') print('|'.join(book_list[t]))
7.实验任务7
程序源代码
data = ['99 81 75', '30 42 90 87', '69 50 96 77 89 93', '82 99 78 100'] list1=[] k=0 for i in data: list1=list1+i.split(' ') for i in range(len(list1)): k+=int(list1[i]) print("%.2f"%(k/len(list1)))
运行结果
8.实验任务8
程序源代码
words_sensitive_list = ['张三', 'V字仇杀队', '杀'] comments_list = ['张三因生命受到威胁正当防卫导致过失杀人,经辩护律师努力,张三不需负刑事责任。', '电影<V字仇杀队>从豆瓣下架了', '娱乐至死'] k=[] for i in range(0,len(comments_list)): k.append(comments_list[i].replace(words_sensitive_list[i],'*'*len(words_sensitive_list[i]))) for t in k: print(t)
实验结果
9.实验任务9
程序源代码
""" 家用电器销售系统 v1.0 """ #欢迎信息 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(' ' ) #用户输入信息 product_id= input('请输入您要购买的产品编号:') price=float(input('请输入您要购买的产品价格:')) count=int(input('请输入您要购买的产品数量:')) #计算金额 print('购买成功,您需要支付',price*count,'元') #退出系统 print('谢谢您的光临,下次再见!')
实验结果
标签:%-,list1,list,len,列表,print,实验,字符串,10s From: https://www.cnblogs.com/yang2002/p/17243055.html