我知道
"123abc" * 2
计算结果为"123abc123abc",但是有一种简单的方法可以将单个字母重复N次,例如转换"123abc"为"112233aabbcc"或"111222333aaabbbccc"吗?
"123abc123abc"
"123abc"
"112233aabbcc"
"111222333aaabbbccc"
关于什么:
>>> s = '123abc' >>> n = 3 >>> ''.join([char*n for char in s]) '111222333aaabbbccc' >>>
(因为在连接中使用listcomp更快,所以从生成器表达式更改为list comp )