实战
1.车牌归属地(切片操作)
# 车牌归属地
lst = ['京A8888', '津B6666', '吉C7777']
for item in lst:
area = item[0:1]
print(item, ' 归属地为:', area)
2.统计字符串出现次数
s = 'HelloPython, HelloJava, hellophp'
word = input('请输入要统计的字符:')
print(s.upper().count(word)) # 不区分大小写(都转成大写)
print('{0}在{1}一共出现了{2}'.format(word, s, s.upper().count(word)))
3. 格式化输出商品信息
lst=[
['01', '电风扇', '美的', 550],
['02', '洗衣机', 'TCL', 2400],
['03', '微波炉', '老票', 5550]
]
print('编号\t\t名称\t\t\t品牌\t\t单价')
for item in lst:
for i in item:
print(i, end='\t\t')
print()
print()
for item in lst:
item[0] = '0000'+item[0]
item[3] = '¥{0:.2f}'.format(item[3])
print(' 编号\t\t 名称\t\t品牌\t\t 单价')
for item in lst:
for i in item:
print(i, end='\t\t')
print()