Jason S Jason S - 5 months ago 8
Python Question

How to repeat individual characters in strings in Python

I know that

"123abc" * 2


evaluates as
"123abc123abc"
, but is there an easy way to repeat individual letters N times, e.g. convert
"123abc"
to
"112233aabbcc"
or
"111222333aaabbbccc"
?

Answer

What about:

>>> s = '123abc'
>>> n = 3
>>> ''.join(char*n for char in s)
'111222333aaabbbccc'
>>>