实验2 字符串和列表

实验任务1

task1.py

实验源码:

 

复制代码
x = 'nba FIFA'
print(x.upper())
print(x.lower())
print(x.swapcase())
print()

x = 'abc'
print(x.center(10,'*'))
print (x.ljust(10,'*'))
print(x.rjust(10,'*'))
print()

x = '123'
print(x.zfill(10))
x = 123
print(str(x).zfill(10))
print()

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))
复制代码

实验结果:

实验任务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

task3.py

实验源码:

复制代码
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()

t = []
i = 0
while i < len(name_list):
    t.append(name_list[i].title())
    i += 1

print('\n'.join(t))
复制代码

实验结果;

实验任务4

task4.py

实验源码:

 

posted @   刘权兴  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示