如何将字符串中的字符替换 n 次,每次都使用给定列表的新字符?

我有以下字符串,我想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 循环方法,但没有完全掌握它的窍门。你们能帮我一下吗?


慕斯709654
浏览 80回答 1
1回答

米脂

你应该使用str.replacebefcodes = ["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
打开App,查看更多内容
随时随地看视频慕课网APP