实验任务1
task1
1 x = 'nba FIFA' 2 print(x.upper()) 3 print(x.lower()) 4 print(x.swapcase()) 5 print() 6 7 x = 'abc' 8 print(x.center(10, '*')) 9 print(x.ljust(10, '*')) 10 print(x.rjust(10, '*')) 11 print() 12 13 x = '123' 14 print(x.zfill(10)) 15 x = 123 16 print(str(x).zfill(10)) 17 print() 18 19 x = 'phone_number' 20 print(x.isidentifier()) 21 x = '222test' 22 print(x.isidentifier()) 23 print() 24 25 x = ' ' 26 print(x.isspace()) 27 x = '\n' 28 print(x.isspace()) 29 print() 30 31 x = 'python is fun' 32 table = x.maketrans('thon', '1234') 33 print(x.translate(table))View Code
实验任务2
task2
1 x = [5, 11, 9, 7, 42] 2 print('整数输出1:', end= '') 3 i = 0 4 while i < len(x): 5 print(x[i], end= ' ') 6 i += 1 7 8 print('\n整数输出2: ', end= '') 9 i = 0 10 while i < len(x): 11 print(f'{x[i]:02d}', end= '-') 12 i += 1 13 14 print('\n整数输出3: ', end= '') 15 i = 0 16 while i < len(x) - 1: 17 print(f'{x[i]:02d}', end= '-') 18 i += 1 19 print(f'{x[-1]:02d}') 20 21 print('\n字符输出1: ', end= '') 22 y1 = [] 23 i = 0 24 while i < len(x): 25 y1.append(str(x[i])) 26 i += 1 27 print('-'.join(y1)) 28 29 print('字符输出2: ', end = '') 30 y2 = [] 31 i = 0 32 while i < len(x): 33 y2.append( str(x[i]).zfill(2) ) 34 i += 1 35 print('-'.join(y2))View Code
当 x = [1, 9, 8, 4, 2, 0, 49]
实验任务3
task3
1 name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan', 'cocteau twins'] 2 i = 0 3 while i < len(name_list): 4 print(name_list[i].title()) 5 i += 1 6 print() 7 8 t = [] 9 i = 0 10 while i < len(name_list): 11 t.append(name_list[i].title()) 12 i += 1 13 print('\n'.join(t))View Code
实验任务4
task4
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))View Code
实验任务5
task5
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 ''' 23 print('行数:', len(x.splitlines())) 24 print('单词数:', len(x.split())) 25 print('字符数:', len(x.capitalize())) 26 print('空格数:', x.count(' '))View Code
实验任务6
task6
book_list = [['静静的顿河','肖洛霍夫','金人', '人民文学出版社'], ['大地之上','罗欣顿.米斯特里','张亦琦', '天地出版社'], ['夜航西飞', '柏瑞尔.马卡姆', '陶立夏', '人民文学出版社'], ['来自民间的叛逆', '袁越', '','新星出版社'], ['科技与恶的距离', '珍妮.克里曼', ' 詹蕎語', '墨刻出版社'], ['灯塔','克里斯多夫.夏布特','吕俊君','北京联合出版公司'], ['小行星掉在下午','沈大成', '', '广西师范大学出版社']] x = '图书信息' print(x.center(40, '*')) i = 0 while i < len(book_list): print(book_list[i]) i += 1 print()View Code
标签:10,list,len,列表,better,print,while,实验,字符串 From: https://www.cnblogs.com/202280060019x/p/17242872.html