1 # 字符串的基础操作 2 # 课堂上没有演示的一些方法 3 4 x = 'nba FIFA' 5 print(x.upper()) #字符串转大写 6 print(x.lower()) #字符串转小写 7 print(x.swapcase()) #字符串大小写翻转 8 print() 9 10 x = 'abc' 11 print(x.center(10,'*')) # 字符串居中,宽度10列,不足左右补*号 12 print(x.ljust(10,'*')) # 字符串居左,宽度10列,不足右边补*号 13 print(x.rjust(10,'*')) # 字符串居右,宽度10列,不足左边补*号 14 15 print() 16 17 x = '123' 18 print(x.zfill(10)) # 字符串宽度10列,不足左边用0填充 19 x = 123 20 print(str(x).zfill(10)) # 把int类型转换成字符串类型后,对字符串对象使用str.zfill()方法 21 print() 22 23 x = 'phone_number' 24 print(x.isidentifier()) # 判断字符串是否是python合法标识符 25 x = '222test' 26 print(x.isidentifier()) 27 print() 28 29 x = ' ' 30 print(x.isspace()) # 判断字符串是否是空白符(包括空格、回车、Tab键) 31 x = '\n' 32 print(x.isspace()) 33 print() 34 35 x = 'python is fun' 36 table = x.maketrans('thon','1234') # 为字符串对象x创建一个字符映射表, 字符thon分别映射到字符1234 37 print(x.maketrans(table)) # 根据字符映射表table对字符串对象x中的字符进行转换
1 #实验任务2 2 # 基础练习: 列表、格式化、类型转换 3 4 x= [1,9,8,4,2,0,49] 5 6 print('整数输出1:', end = '') 7 i = 0 8 while i < len(x): 9 print(x[i], end = '') 10 i += 1 11 12 print('\n整数输出2:', end = '') 13 i = 0 14 while i < len(x): 15 print(f'{x[i]:02d}', end = '-') # 指定每个整数宽度占2列;不足2列,左边补0 16 i += 1 17 18 print('\n整数输出3:',end = '') 19 i = 0 20 while i < len(x) - 1: 21 print(f'{x[i]:02d}',end = '-') 22 i += 1 23 print(f'{x[-1]:02d}') 24 25 print('\n字符输出1:', end = '') 26 y1 = [] 27 i = 0 28 while i < len(x): 29 y1.append(str(x[i])) # 函数str()用于把其它类型对象转换成字符串对象 30 i += 1 31 print('-'.join(y1)) 32 33 print('字符输出2:',end = '') 34 y2 = [] 35 i = 0 36 while i < len(x): 37 y2.append(str(x[i]).zfill(2)) #x[i]是int类型对象,使用str()转换成字符串对象后,处理,然后加入到列表y中 38 i += 1 39 print('-'.join(y2))
1 # 实验任务3 2 # 把姓名转换成大写,便利分行输出. 3 4 name_list = ['daid bowie','louis armstrong','leonard cohen','bob dylan', 'cocteau twins'] 5 #方法1 6 i = 0 7 while i < len(name_list): 8 print(name_list[1].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)) 21 22 print('\n'.join(t))
1 name_list = ['daid bowie','louis armstrong','leonard cohen','bob dylan', 'cocteau twins'] 2 name_list1 = sorted(name_list) 3 i = 0 4 while i < len(name_list1): 5 print(name_list1[i].title()) 6 i += 1
1 x = '''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 c3 = 1+ x.count('\n') 23 print(c3) 24 print(len(x.split())) 25 c1 = c2 = 0 26 for i in x: 27 if 'a'<= i <= 'z' or 'A'<=i <= 'Z': 28 c1 += 1 29 else: 30 c2 += 1 31 print(c1+c2) 32 print(x.count(' '))
1 book_list = [['静静的顿河','肖洛霍夫','金人', '人民文学出版社'], 2 ['大地之上','罗欣顿.米斯特里','张亦琦', '天地出版社'], 3 ['夜航西飞', '柏瑞尔.马卡姆', '陶立夏', '人民文学出版社'], 4 ['来自民间的叛逆', '袁越', '','新星出版社'], 5 ['科技与恶的距离', '珍妮.克里曼', ' 詹蕎語', '墨刻出版社'], 6 ['灯塔','克里斯多夫.夏布特','吕俊君','北京联合出版公司'], 7 ['小行星掉在下午','沈大成', '', '广西师范大学出版社']] 8 g = [0] 9 n = range(1,9) 10 for i in book_list : 11 g.append( '|'.join(i)) 12 i = 0 13 while i <= 7: 14 if i == 0: 15 print('{:*^39}'.format('图书信息')) 16 i += 1 17 else: 18 print(str(i) + '.' +str(g[i])) 19 i += 1
1 data = ['99 81 75', '30 42 90 87', '69 50 96 77 89 93', '82 99 78 100'] 2 x = 0 3 y = 0 4 for i in data: 5 k = i.split() 6 for m in k: 7 x = x + int(m) 8 y += 1 9 z = x/y 10 print(f'{z:.2f}')
1 words_sensitive_list = ['张三', 'V字仇杀队', '杀'] 2 comments_list = ['张三因生命受到威胁正当防卫导致过失杀人,经辩护律师努力,张三不需负刑事责任。','电影<V字仇杀队>从豆瓣下架了','娱乐至死'] 3 y = comments_list[0].replace('张三','**') 4 x = comments_list[1].replace('V字仇杀队','*****') 5 z = comments_list[2].replace('杀','*') 6 print(y + '\n' + x + '\n' + z)
1 #欢迎信息 2 print('欢迎使用家电销售系统!') 3 4 #产品信息列表 5 print('产品和价格信息如下:') 6 print('*'*60) 7 print('%-10s' %'编号','%-10s' %'名称','%-10s' %'品牌', '%-10s' %'价格', '%-10s' %'库存数量') 8 print('-'*60) 9 print('%-10s' %'0001','%-10s' %'电视机','%-10s' %'海尔', '%10.2f' %5999.00, '%-10d' %20) 10 print('%-10s' %'0002','%-10s' %'冰箱','%-10s' %'西门子', '%10.2f' %6998.00, '%-10d' %15) 11 print('%-10s' %'0003','%-10s' %'洗衣机','%-10s' %'小天鹅', '%10.2f' %1999.00, '%-10d' %1) 12 print('%-10s' %'0004','%-10s' %'空调','%-10s' %'格力', '%10.2f' %3900.00, '%-10d' %0) 13 print('%-10s' %'0005','%-10s' %'热水器','%-10s' %'美的', '%10.2f' %688.00, '%-10d' %30) 14 print('%-10s' %'0006','%-10s' %'笔记本','%-10s' %'联想', '%10.2f' %5699.00, '%-10d' %20) 15 print('%-10s' %'0007','%-10s' %'微波炉','%-10s' %'苏泊尔', '%10.2f' %480.50, '%-10d' %33) 16 print('%-10s' %'0008','%-10s' %'投影仪','%-10s' %'松下', '%10.2f' %1250.00, '%-10d' %12) 17 print('%-10s' %'0009','%-10s' %'吸尘器','%-10s' %'飞利浦', '%10.2f' %999.00, '%-10d' %9) 18 print('-'*60) 19 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 33 #用户输入信息 34 product_id=input('请输入您要购买的产品编号:') 35 count=int(input('请输入您要购买的产品数量:')) 36 37 #获取编号对应商品的信息 38 product_index = int(product_id)-1 39 product = products[product_index] 40 41 42 #计算金额 43 print('购买成功,您需要支付',"%.1f"%(int(float(product[3]))*count),'元') 44 45 #退出系统 46 print('谢谢您的光临,下次再见!')
1 """ 2 家用电器销售系统 3 v1.1 4 """ 5 #欢迎信息 6 print('欢迎使用家电销售系统!') 7 8 9 #商品数据 10 products = [ 11 ['0001','电视机','海尔', '5999.00','20'], 12 ['0002','冰箱','西门子', '6998.00','15'], 13 ['0003','洗衣机','小天鹅', '1999.00','10'], 14 ['0004','空调','格力', '3900.00','0'], 15 ['0005','热水器','格力', '688.00','30'], 16 ['0006','笔记本','联想', '5699.00','10'], 17 ['0007','微波炉','苏泊尔', '480.00','33'], 18 ['0008','投影仪','松下', '1250.00','12'], 19 ['0009','吸尘器','飞利浦', '999.00','9'], 20 ] 21 22 #商品信息 23 print('产品和价格信息如下:') 24 print('*'*60) 25 print(f'{"编号":<10}{"名称":<10}{"品牌":<10}{"价格":<10}{"库存数量":<10}') 26 print('-'*60) 27 print(f'{products[0][0]:<10}{products[0][1]:<10}{products[0][2]:<10}{float(products[0][3]):<10.2f}{products[0][4]:<10}') 28 print(f'{products[1][0]:<10}{products[1][1]:<10}{products[1][2]:<10}{float(products[1][3]):<10.2f}{products[1][4]:<10}') 29 print(f'{products[2][0]:<10}{products[2][1]:<10}{products[2][2]:<10}{float(products[2][3]):<10.2f}{products[2][4]:<10}') 30 print(f'{products[3][0]:<10}{products[3][1]:<10}{products[3][2]:<10}{float(products[3][3]):<10.2f}{products[3][4]:<10}') 31 print(f'{products[4][0]:<10}{products[4][1]:<10}{products[4][2]:<10}{float(products[4][3]):<10.2f}{products[4][4]:<10}') 32 print(f'{products[5][0]:<10}{products[5][1]:<10}{products[5][2]:<10}{float(products[5][3]):<10.2f}{products[5][4]:<10}') 33 print(f'{products[6][0]:<10}{products[6][1]:<10}{products[6][2]:<10}{float(products[6][3]):<10.2f}{products[6][4]:<10}') 34 print(f'{products[7][0]:<10}{products[7][1]:<10}{products[7][2]:<10}{float(products[7][3]):<10.2f}{products[7][4]:<10}') 35 print(f'{products[8][0]:<10}{products[8][1]:<10}{products[8][2]:<10}{float(products[8][3]):<10.2f}{products[8][4]:<10}') 36 print('-'*60) 37 38 #用户输入信息 39 product_id=input('请输入您要购买的产品编号:') 40 count=int(input('请输入您要购买的产品数量:')) 41 42 #获取编号对应商品的信息 43 product_index = int(product_id)-1 44 product = products[product_index] 45 46 47 #计算金额 48 print('购买成功,您需要支付',"%.1f"%(int(float(product[3]))*count),'元') 49 50 #退出系统 51 print('谢谢您的光临,下次再见!')
实验总结:
认识到了:while语句和for语句,以及if后面要加上“:”,break和continue在循环里面的区别;[]列表可以通过append()函数给扩充,可以把切片放在这个了表里面;.split()函数在某些列表中的作用;最后是复习了f-string函数的用法