task1
1 x="nba FIFA" 2 print(x.upper()) 3 print(x.lower()) 4 print(x.swapcase()) 5 print() 6 x="abc" 7 print(x.center(10,'*')) 8 print(x.ljust(10,'*')) 9 print(x.rjust(10,'*')) 10 x="123" 11 print(x.zfill(10)) 12 x=123 13 print(str(x).zfill(10)) 14 print() 15 x="phone_number" 16 print(x.isidentifier()) 17 x="222test" 18 print(x.isidentifier()) 19 print() 20 x=" " 21 print(x.isspace()) 22 x='\n' 23 print(x.isspace()) 24 print() 25 x="python is fun" 26 table = x.maketrans("thon","1234") 27 print(x.translate(table))
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))
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 7 print() 8 9 t=[] 10 i = 0 11 while i <len(name_list): 12 t.append(name_list[i].title()) 13 i+=1 14 15 print('\n'.join(t))
task4
1 name_list = ['david bowie','louis armstrong','leonard cohen','bob dylan','cocteau twins'] 2 x=sorted(name_list) 3 i = 0 4 while i < len(x): 5 print(x[i].title()) 6 i+=1
task5
1 import this 2 text = ('''The Zen of Python, by Tim Peters 3 4 Beautiful is better than ugly. 5 Explicit is better than implicit. 6 Simple is better than complex. 7 Complex is better than complicated. 8 Flat is better than nested. 9 Sparse is better than dense. 10 Readability counts. 11 Special cases aren't special enough to break the rules. 12 Although practicality beats purity. 13 Errors should never pass silently. 14 Unless explicitly silenced. 15 In the face of ambiguity, refuse the temptation to guess. 16 There should be one-- and preferably only one --obvious way to do it. 17 Although that way may not be obvious at first unless you're Dutch. 18 Now is better than never. 19 Although never is often better than *right* now. 20 If the implementation is hard to explain, it's a bad idea. 21 If the implementation is easy to explain, it may be a good idea. 22 Namespaces are one honking great idea -- let's do more of those!''') 23 line = 0 24 word = 0 25 string = 0 26 space = 0 27 line = len(text.splitlines()) 28 word = len(text.split()) 29 string = len(text) 30 for i in text: 31 if i == " ": 32 space += 1 33 print('行数:{}'.format(line)) 34 print('字数:{}'.format(word)) 35 print('字符数:{}'.format(string)) 36 print('空格数:{}'.format(space))
标签:10,text,len,better,实验,print,than From: https://www.cnblogs.com/bhsyyds/p/17277264.html