Jas*_*oal 5 python string-concatenation
我想要实现的很简单,在 RI 中可以做类似的事情
paste0("https\\",1:10,"whatever",11:20),
如何在 Python 中做到这一点?我在这里找到了一些东西,但只允许:
paste0("https\\",1:10).
任何人都知道如何解决这个问题,这一定很容易做到,但我找不到方法。
@Jason,我建议您使用以下两种方法中的任何一种来完成此任务。
\n\n\xe2\x9c\x93 通过使用列表理解和zip()函数创建文本列表。
\n\n\n\n\n注意:要
\n\n\\在屏幕上打印,请使用转义序列\\\\。请参阅转义序列列表及其用途。如果您认为此答案不能满足您的问题,请发表评论。我将根据您的输入和预期输出更改答案。
\n
texts = ["https\\\\\\\\" + str(num1) + "whatever" + str(num2) for num1, num2 in zip(range(1,10),range(11, 20))]\n\nfor text in texts:\n print(text)\n\n"""\nhttps\\\\1whatever11\nhttps\\\\2whatever12\nhttps\\\\3whatever13\nhttps\\\\4whatever14\nhttps\\\\5whatever15\nhttps\\\\6whatever16\nhttps\\\\7whatever17\nhttps\\\\8whatever18\nhttps\\\\9whatever19\n"""\nRun Code Online (Sandbox Code Playgroud)\n\n\xe2\x9c\x93 通过定义一个简单的函数 paste0()来实现上述逻辑以返回文本列表。
\n\nimport json\n\ndef paste0(string1, range1, strring2, range2):\n texts = [string1 + str(num1) + string2 + str(num2) for num1, num2 in zip(range1, range2)]\n\n return texts\n\n\ntexts = paste0("https\\\\\\\\", range(1, 10), "whatever", range(11, 20))\n\n# Pretty printing the obtained list of texts using Jon module\nprint(json.dumps(texts, indent=4))\n\n"""\n[\n "https\\\\\\\\1whatever11",\n "https\\\\\\\\2whatever12",\n "https\\\\\\\\3whatever13",\n "https\\\\\\\\4whatever14",\n "https\\\\\\\\5whatever15",\n "https\\\\\\\\6whatever16",\n "https\\\\\\\\7whatever17",\n "https\\\\\\\\8whatever18",\n "https\\\\\\\\9whatever19"\n]\n"""\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
11821 次 |
| 最近记录: |