实验2
task1
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))
运行结果


task2
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))
实验结果

task3
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()
实验结果

task4
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()
实验结果


浙公网安备 33010602011771号