2 回答
TA贡献1827条经验 获得超7个赞
用 f-string 打印
f-Strings:一种在 Python 中格式化字符串的新改进方法
PEP 498 - 文字字符串插值
# replace print(x,'runs through',+y+ '.')
# with
print(f'{x} runs through {y}.'
# or with
print(x,'runs through ' +y+ '.') # note the added space after through and the removal of the ,
更新脚本
express_file = {'TPLEX':'Pangasinan', 'SLEX':'Subic', 'Cavitex':'Bacoor,Cavite','MCX':'Muntinlupa','Star Tollway':'Laguna'}
for x,y in express_file.items():
print(f'{x} runs through {y}.')
print('The following Expressway are included in this data set:')
for x in express_file.keys():
print(x)
print('\nThe following Provinces are included in this data set:')
for x in express_file.values():
print(x)
[out]:
TPLEX runs through Pangasinan.
SLEX runs through Subic.
Cavitex runs through Bacoor,Cavite.
MCX runs through Muntinlupa.
Star Tollway runs through Laguna.
The following Expressway are included in this data set:
TPLEX
SLEX
Cavitex
MCX
Star Tollway
The following Provinces are included in this data set:
Pangasinan
Subic
Bacoor,Cavite
Muntinlupa
Laguna
TA贡献1946条经验 获得超3个赞
express_file = {'TPLEX':'Pangasinan', 'SLEX':'Subic', 'Cavitex':'Bacoor,Cavite','MCX':'Muntinlupa','Star Tollway':'Laguna'}
for x,y in express_file.items():
print(x,'runs through'+y+ '.')
print('The following Expressway are included in this data set:')
for x in express_file.keys():
print(x)
print('\nThe following Provinces are included in this data set:')
for x in express_file.values():
print(x)
您应该删除 +y+ 旁边的逗号。
添加回答
举报