我有以下字符串,我想A1用列表中的每个字符替换befcodes并打印它:befcodes = ["A1","A2","A3","A4","A5","A6","A7","A8","A9","10","11","12","13","14","15","16","17","18","19","20"]telegram = "$00;02;A1;00000000*49"我想得到如下所示的输出:$00;02;A1;00000000*49$00;02;A2;00000000*49$00;02;A3;00000000*49......$00;02;19;00000000*49$00;02;20;00000000*49我尝试了几种不同的字符串格式化和 for 循环方法,但没有完全掌握它的窍门。你们能帮我一下吗?
1 回答
米脂
TA贡献1836条经验 获得超3个赞
你应该使用str.replace
befcodes = ["A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"]
telegram = "$00;02;A1;00000000*49"
for code in befcodes:
print(telegram.replace("A1", code))
给予
$00;02;A1;00000000*49
$00;02;A2;00000000*49
$00;02;A3;00000000*49
...
$00;02;18;00000000*49
$00;02;19;00000000*49
$00;02;20;00000000*49
- 1 回答
- 0 关注
- 77 浏览
添加回答
举报
0/150
提交
取消