首页 > 其他分享 >实验2 字符串和列表

实验2 字符串和列表

时间:2023-03-27 23:34:19浏览次数:23  
标签:%- name list 列表 print 实验 字符串 10s

实验任务1

编译源代码

#task1.py

# 字符串的基础操作
# 课堂上没有演示的一些方法

x = 'nba FIFA'
print(x.upper()) # 字符串转大写
print(x.lower()) # 字符串转小写
print(x.swapcase()) # 字符串大小写翻转
print()

x = 'abc'
print(x.center(10, '*')) # 字符串居中,宽度10列,不足左右补*号
print(x.ljust(10, '*')) # 字符串居左,宽度10列,不足右边补*号
print(x.rjust(10, '*')) # 字符串居右,宽度10列,不足左边补*号
print()

x = '123'
print(x.zfill(10)) # 字符串宽度10列,不足左边用0填充
x = 123
print(str(x).zfill(10)) # 把int类型转换成字符串类型后,对字符串对象使用str.zfill()方法
print()

x = 'phone_number'
print(x.isidentifier()) # 判断字符串是否是python合法标识符
x = '222test'
print(x.isidentifier())
print()

x = ' '
print(x.isspace()) # 判断字符串是否是空白符(包括空格、回车、Tab键)
x = '\n'
print(x.isspace())
print()

x = 'python is fun'
table = x.maketrans('thon', '1234') # 为字符串对象x创建一个字符映射表, 字符thon分别映射到字符1234
print(x.translate(table)) # 根据字符映射表table对字符串对象x中的字符进行转换

运行结果截图

 

实验任务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 = '_')#指定每个整数宽度占2列:不足2列,右边补0
    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])) #函数str()用于把其他类型对象转换成字符串对象
    i += 1
print('-'.join(y1))


print('字符输出2:',end = '')
y2=[]
i = 0
while i < len(x):
    y2.append(str(x[i]).zfill(2))# x[i]是int类型对象,使用str()转换成字符串对象后,处理,然后加入到列表y中
    i += 1
print('-'.join(y2))

运行结果截图

 

 

实验任务3

编译源代码

#task3.py

#把姓名转换成大写,遍历分行输出

name_list = ['david bowie','louis armstrong','leonard cohen','bob dylan','cocteau twins']

#方法1
i = 0
while i < len(name_list):
    print(name_list[i].title())
    i += 1

print()

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

print('\n'.join(t))

运行结果截图

 

实验任务4

编译源代码

 

name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan', 'cocteau twins']
name_list = [name.title() for name in name_list]
name_list.sort()
for i, name in enumerate(name_list):
    print(f"{i+1}. {name}")

运行结果截图

 

实验任务5

编译源代码

s ='''The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!'''
a = len(s.splitlines())
b = len(s)
c = len(s.split())
d = s.count(' ')
print(f'行数:{a}')
print(f'字符数:{b}')
print(f'字符数:{c}')
print(f'空格数:{d}')

运行结果截图

 

实验任务6

编译源代码

# 图书列表中每行信息: 书名、作者、译者、出版社
# 如果是国内作者,则译者字段为空串

book_list = [['静静的顿河','肖洛霍夫','金人', '人民文学出版社'],
             ['大地之上','罗欣顿.米斯特里','张亦琦', '天地出版社'],
             ['夜航西飞', '柏瑞尔.马卡姆', '陶立夏', '人民文学出版社'],
             ['来自民间的叛逆', '袁越', '','新星出版社'],
             ['科技与恶的距离', '珍妮.克里曼', ' 詹蕎語', '墨刻出版社'],
             ['灯塔','克里斯多夫.夏布特','吕俊君','北京联合出版公司'],
             ['小行星掉在下午','沈大成', '', '广西师范大学出版社']]

print('图书信息'.center(40,'*'))
i = 0
while i < len(book_list):
    print(i+1, '.', '|'.join(book_list[i]))
    i += 1

 

实验任务7

编译源代码

'''
某.csv格式数据文件内数据如下:
99 81 75
30 42 90 87
69 50 96 77 89, 93
82, 99, 78, 100
'''
data = ['99 81 75', '30 42 90 87', '69 50 96 77 89 93', '82 99 78 100']
data_str = ','.join(data)   #转换为字符串
data_str1 = data_str.replace(',',' ')    #转化为都是逗号隔开的字符串
n = data_str1.split()       #转化为list类型
i = 0
sum = 0
while i < len(n):
    sum += int(n[i])
    i += 1
average = sum/len(n)
print(f'{average:.2f}')

 

实验任务8

编译源代码

words_sensitive_list = ['张三', 'V字仇杀队', '杀']
comments_list = ['张三因生命受到威胁正当防卫导致过失杀人,经辩护律师努力,张三不需负刑事责任。',
                 '电影<V字仇杀队>从豆瓣下架了',
                 '娱乐至死']
comments_str = ','.join(comments_list)
comments_str1 = comments_str.replace('张三','**').replace('V字仇杀队','*****').replace('杀','*')
comments_str2 = comments_str1.replace(',','\n')
print(comments_str2)

运行结果截图

 

实验任务9

编译源代码

"""
家用电器销售系统
v1.0
"""
#欢迎信息
print('欢迎使用家用电器销售网络!')

#产品信息列表
print('产品和价格信息如下:')
print('*'*60)
print('%-10s'%'编号', '%-10s'%'名称', '%-10s'%'品牌', '%-10s'%'价格', '%-10s'%'库存数量')
print('-'*60)
print('')
print('%-10s'%'0001', '%-10s'%'电视机', '%-10s'%'海尔', '%10.2f'%5999.00, '%10d'%20)
print('%-10s'%'0002', '%-10s'%'冰箱', '%-10s'%'西门子', '%10.2f'%6998.00, '%10d'%15)
print('%-10s'%'0003', '%-10s'%'洗衣机', '%-10s'%'小天鹅', '%10.2f'%1999.00, '%10d'%10)
print('%-10s'%'0004', '%-10s'%'空调', '%-10s'%'格力', '%10.2f'%3900.00, '%10d'%0)
print('%-10s'%'0005', '%-10s'%'热水器', '%-10s'%'美的', '%10.2f'%688.00, '%10d'%30)
print('%-10s'%'0006', '%-10s'%'笔记本', '%-10s'%'联想', '%10.2f'%5699.00, '%10d'%10)
print('%-10s'%'0007', '%-10s'%'微波炉', '%-10s'%'苏泊尔', '%10.2f'%480.50, '%10d'%33)
print('%-10s'%'0008', '%-10s'%'投影仪', '%-10s'%'松下', '%10.2f'%1250.00, '%10d'%12)
print('%-10s'%'0009', '%-10s'%'吸尘器', '%-10s'%'飞利浦', '%10.2f'%999.00, '%10d'%9)
print('-'*60)

#用户输入信息
product_id = input('请输入你购买产品的序号:')
price = float(input('请输入你要购买的产品价格:'))
count = int(input('请输入你要购买的产品数量'))

#计算金额
print('购买成功,你需要支付',price*count,'元')

#退出系统
print('谢谢你的光临,下次再见!')

运行结果截图

 

标签:%-,name,list,列表,print,实验,字符串,10s
From: https://www.cnblogs.com/zz666666/p/17243170.html

相关文章

  • 实验二,字符串和列表
    试验任务1:在python开发环境下,新建一个.py源文件,输入并运行以下代码,体验这里用到的字符串的方法。task1.py实验源码:#字符串的基础操作#课堂上没有演示的一些方法x='n......
  • 实验2 字符串和列表
    实验任务1task1.py实验源码1x='nbaFIFA'2print(x.upper())3print(x.lower())4print(x.swapcase())5print()67x='abc'8print(x.center(10,......
  • 实验2
    1.实验任务1task1.pyx='nbaFIFA'print(x.upper())#字符串转大写print(x.lower())#字符串转小写print(x.swapcase())#字符串大小写翻转print()x='abc'......
  • 支付回调MQ消息的幂等处理及MD5字符串es中的使用及支付宝预授权完成
    支付回调MQ消息的幂等处理及MD5字符串es中的使用及支付宝预授权完成1.幂等的处理,根据对象的转json转md5作为key,退款的处理控制发送端?业务上比较难控制。支付异步通知,......
  • 实验二——字符串,列表
    任务一程序:1x='nbaFIFA'2print(x.upper())3print(x.lower())4print(x.swapcase())#大小写翻转56x='abc'7print(x.center(10,'*'))8pri......
  • 实验一 密码引擎-2-电子钥匙功能测试
    目录1解压"资源"中“龙脉密码钥匙驱动实例工具等”压缩包2在Ubuntu中运行“龙脉密码钥匙驱动实例工具等\mToken-GM3000\skf\samples\linux_mac”中例程,提交运行结果截图......
  • Spinner(列表选项框)的基本使用
    这一节是想给大家介绍一个Gallery(画廊)的一个控件,尽管我们可以不通过兼容使用Gallery,不过想想还是算了,因为Gallery在每次切换图片的时候,都需要重新创建视图,这样无疑会造成......
  • 实验一 密码引擎-2-电子钥匙功能测试
    在Ubuntu中运行“龙脉密码钥匙驱动实例工具等\mToken-GM3000\skf\samples\linux_mac”中例程,提交运行结果截图加分项:运行“龙脉密码钥匙驱动实例工具等\mToken-GM3000......
  • dom4j 解析xml string 字符串
    packagedom4j;importjava.util.Iterator;importorg.dom4j.Document;importorg.dom4j.DocumentException;importorg.dom4j.DocumentHelper;importorg.dom4j.......
  • 实验一 密码引擎-2-电子钥匙功能测试
    任务详情0参考附件中的视频1解压"资源"中“龙脉密码钥匙驱动实例工具等”压缩包2在Ubuntu中运行“龙脉密码钥匙驱动实例工具等\mToken-GM3000\skf\samples\linux_ma......