1.实验任务1
task1.py
程序源代码:
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 x = 'python is fun' 31 table = x.maketrans('thon', '1234') 32 print(x.translate(table))
运行程序截图:
2.实验任务2
task2.py
程序源代码:
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
task3.py
程序源代码:
1 name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan', 2 'cocteau twins'] 3 #1 4 i = 0 5 while i <len(name_list): 6 print(name_list[i].title()) 7 i +=1 8 9 print() 10 11 #2 12 t = [] 13 i = 0 14 while i <len(name_list): 15 t.append(name_list[i].title()) 16 i +=1 17 18 print('\n'.join(t))
运行程序截图:
4.实验任务4
task4.py
程序源代码:
name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan', 'cocteautwins'] new_namelist = sorted(name_list) i = 0 while i<len(new_namelist): n = i +1 print('{}.'.format(n),end = '') print(new_namelist[i].title()) i +=1 print()
运行程序截图: