实验2 字符串与列表

发布时间 2023-03-22 21:13:54作者: 魅力哥
 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))

运行结果:

 

 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))

运行结果:

 

更换数据后:

 

 task3.py

1 name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan',
2 'cocteau twins']
3 i = 0
4 while i < len(name_list):
5     print(name_list[i].title())
6     i += 1
7 print()

运行结果:

 

 task4.py