python3学习之路23实操练习9字符串函数

导读:本篇文章讲解 python3学习之路23实操练习9字符串函数,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

在这里插入图片描述

# 实操案例9 -- 任务1 -- 统计字符串中出现指定字符的次数(忽略大小写) - 函数
def get_count(s,ch): # s 字符串,ch表示要统计的字符
    count = 0
    for item in s:
        if ch.upper() == item or ch.lower() == item:
            count += 1
    return count
if __name__=='__main__':
    s = 'hellopYthon,helloJAva,hellogo'
    ch = input('请输入要统计的字符:')
    count = get_count(s,ch)
    print(f'{ch}{s}中出现的次数为:{count}')
# 实操案例9 -- 任务2 -- 格式化输出商品的名称 - 字符串格式化
#商品信息用列表保存
lst = [['01','电风扇','美的',500],
        ['02','洗衣机','TCL',1000],
        ['03','微波炉','老板',400]]
print('编号\t\t名称\t\t品牌\t\t单价')
for item in lst:
    for i in item:
        print(i,end='\t\t')
    print()
print('--------------字符串格式化------------')
print('编号\t\t名称\t\t品牌\t\t单价')
for item in lst:
    item[0]='0000'+item[0]
    item[3]='${:.2f}'.format(item[3])
for item in lst:
    for i in item:
        print(i,end='\t')
    print()
# 实操案例9 -- 任务2 -- 格式化输出商品的名称 - 因为存在重复的代码,所以可以用函数
def show(lst):
    for item in lst:
        for i in item:
            print(i, end='\t\t')
        print()

lst = [['01','电风扇','美的',500],
        ['02','洗衣机','TCL',1000],
        ['03','微波炉','老板',400]]
print('编号\t\t名称\t\t\t品牌\t\t单价')
show(lst)

print('-----------------字符串格式化---------------')
print('编号\t\t\t名称\t\t\t品牌\t\t\t单价')
for item in lst:
    item[0]='0000'+item[0]
    item[3]='${:.2f}'.format(item[3])
show(lst)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/1898.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!