统计字符串中出现指定字符的次数---忽略大小写-使用函数实现--字符串的使用
def get_count(s,ch): #输入的字符串和需要统计的字符
count=0 #用来次数统计的
for item in s:
if ch.upper()==item or ch.lower()==item:
count+=1
return count
if __name__=='__main__':
s='helloworld,hellppython,hellohp'
ch=input('请输入你需要统计的字符:')
count=get_count(s,ch)
print(f'{ch}在 {s}中出现的次数为:{count}')
格式化输出商品的名称和单价 ---字符串的格式化+列表+函数
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单价')
for item in lst:#item是lst的内容
item[0]='0000'+item[0] #使用索引课查看内容
item[3]='¥{:.2f}'.format(item[3])
show(lst)