45

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"?

Chris Mueller
  • 6,490
  • 5
  • 29
  • 35
Jason S
  • 184,598
  • 164
  • 608
  • 970

10 Answers10

54

What about:

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

(changed to a list comp from a generator expression as using a list comp inside join is faster)

Bahrom
  • 4,752
  • 32
  • 41
8

If you want to repeat individual letters you can just replace the letter with n letters e.g.

>>> s = 'abcde'
>>> s.replace('b', 'b'*5, 1)
'abbbbbcde'
ssmid
  • 350
  • 2
  • 12
7

An alternative itertools-problem-overcomplicating-style option with repeat(), izip() and chain():

>>> from itertools import repeat, izip, chain
>>> "".join(chain(*izip(*repeat(s, 2))))
'112233aabbcc'
>>> "".join(chain(*izip(*repeat(s, 3))))
'111222333aaabbbccc'

Or, "I know regexes and I'll use it for everything"-style option:

>>> import re
>>> n = 2
>>> re.sub(".", lambda x: x.group() * n, s)  # or re.sub('(.)', r'\1' * n, s) - thanks Eduardo
'112233aabbcc'

Of course, don't use these solutions in practice.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
5

Or another way to do it would be using map:

"".join(map(lambda x: x*7, "map"))
sarnthil
  • 395
  • 2
  • 7
2

Or using regular expressions:

>>> import re
>>> s = '123abc'
>>> n = 3
>>> re.sub('(.)', r'\1' * n, s)
'111222333aaabbbccc'
2

And since I use numpy for everything, here we go:

import numpy as np
n = 4
''.join(np.array(list(st*n)).reshape(n, -1).T.ravel())
Gerges
  • 6,269
  • 2
  • 22
  • 44
2

here is my naive solution

text = "123abc"
result = ''
for letters in text:
    result += letters*3

print(result)

output: 111222333aaabbbccc

1

@Bahrom's answer is probably clearer than mine, but just to say that there are many solutions to this problem:

>>> s = '123abc'
>>> n = 3
>>> reduce(lambda s0, c: s0 + c*n, s, "")
'111222333aaabbbccc'

Note that reduce is not a built-in in python 3, and you have to use functools.reduce instead.

julienc
  • 19,087
  • 17
  • 82
  • 82
1

Another way:

def letter_repeater(n, string):
    word = ''
    for char in list(string):
        word += char * n
    print word

letter_repeater(4, 'monkeys')


mmmmoooonnnnkkkkeeeeyyyyssss
af3ld
  • 782
  • 8
  • 30
  • yeah, that's the obvious imperative way, I figured there was something more Pythonic though. (I'd recommend `return word` rather than `print word` by the way) – Jason S Jul 08 '16 at 19:00
  • @JasonS, I think the other options given are more pythonic, but it gets the job done. And agreed on the `return` vs. `print`, just thought it would be quicker to get the point across – af3ld Jul 08 '16 at 19:07
  • 1
    p.s. you also don't need to use `list(string)` -- strings are iterable already. (And I did upvote you btw!) – Jason S Jul 08 '16 at 19:09
0

Python:

def  LetterRepeater(times,word) :
    word1=''
    for letters in word:
        word1 += letters * times
    print(word1)

    word=input('Write down the word :   ')
    times=int(input('How many times you want to replicate the letters ?    '))
    LetterRepeater(times,word)
kadamb
  • 1,532
  • 3
  • 29
  • 55
Ata Reenes
  • 188
  • 1
  • 10
  • 5
    Welcome. It looks like this needs some additional care in terms of the indentation—and especially since this is python. Can you edit it? Also, as this question already has nine other answers—including [an accepted answer with 34 upvotes](https://stackoverflow.com/a/38273369/3025856)—please be sure to explain how your approach differs from existing approaches, and why your approach might be preferred. – Jeremy Caney Nov 08 '20 at 21:42