# Repeat string to certain length

What is an efficient way to repeat a string to a certain length? Eg: repeat(‘abc’, 7) -> ‘abcabca’

Here is my current code:

```def repeat(string, length):
cur, old = 1, string
while len(string) < length:
string += old[cur-1]
cur = (cur+1)%len(old)
return string
```

Is there a better (more pythonic) way to do this? Maybe using list comprehension?

Repeat array to a certain length?

I’m having an array for example with 4 elements array(a, b, c, d); what is the fastest way to repeat this array to create a new array with a certain length, e.g 71 elements?

repeat the string sequence until it reaches a certain length in java

I have two strings plainText and key. Now, if the plainText.length > key.length then i have to repeat the key until its the same length as that of plainText. here’s an example: plainText = hellowo

Turning String of Length X -> Length Y [duplicate]

This question already has an answer here: Repeat string to certain length 12 answers I have two strings in variables x and y, where the length of x is greater than the length of y. How can you

Truncating a string to certain length, and then adding characters to the end in C

If a string is greater than a certain length, I want to truncate it to a certain length, and replace the last 3 characters with a period (.). What I have right now causes a segmentation fault: #defi

Repeat String – Javascript

What is the best or most concise method for returning a string repeated an arbitrary amount of times? The following is my best shot so far: function repeat(s, n){ var a = []; while(a.length < n){ a

Repeat String – Javascript

What is the best or most concise method for returning a string repeated an arbitrary amount of times? The following is my best shot so far: function repeat(s, n){ var a = []; while(a.length < n){ a

Search and replace string of certain length using Perl regex

I am trying to search and replace a string that is up to and including 3 characters in length with nothing (so ‘deleting’ the element contents). So I have something like: foreach (@array) { s/^{1,3}\$

Alphanumeric Increment a string in PHP (To a certain length)

I need to generate a sequence (or function to get a next id) with an alphanumeric incrementor. The length of the string must be defineable, and the Characters must be 0-9, A-Z. So for example, with

permutations of a certain length

I am trying to find the permutations of a string of a certain length. ex: java –> permutations of length 2 ja,jv,ja | aj,av,aa | vj, va, va | aj, aa, av What I am having trouble with right now i

Replace a string matched by certain regexp with its length in Vim

In Vim, how do I replace a string matched by certain regexp with its length, like this: Regexp = /w* hello -> 5 bye -> 3 Is it possible to do it with :s or do I need to write some s

## Answers

```def repeat_to_length(string_to_expand, length):
return (string_to_expand * ((length/len(string_to_expand))+1))[:length]
```

Repeating a string a fixed number of times is a built-in operation:

```'abc' * 7
```

So, just calculate the number of repeats you need to reach the length you want, and put that on the RHS. You’ll then need to trim it to the right length.

(It appears that this is what the other answer does, but a little bit more explanation seemed useful.)

How about string * (length / len(string)) + string[0:(length % len(string))]

This is one way to do it using a list comprehension, though it’s increasingly wasteful as the length of the rpt string increases.

```def repeat(rpt, length):
return ''.join([rpt for x in range(0, (len(rpt) % length))])[:length]
```
```from itertools import cycle, islice
def srepeat(string, n):
return ''.join(islice(cycle(string), n))
```
```def rep(s, m):
a, b = divmod(m, len(s))
return s * a + s[:b]
```

Yay recursion!

```def trunc(s,l):
if l > 0:
return s[:l] + trunc(s, l - len(s))
return ''
```

Won’t scale forever, but it’s fine for smaller strings. And it’s pretty.

I admit I just read the Little Schemer and I like recursion right now.

Perhaps not the most efficient solution, but certainly short & simple:

```def repstr(string, length):
return (string * length)[0:length]

repstr("foobar", 14)
```

Gives “foobarfoobarfo”. One thing about this version is that if length < len(string) then the output string will be truncated. For example:

```repstr("foobar", 3)
```

Gives “foo”.

Edit: actually to my surprise, this is faster than the currently accepted solution (the ‘repeat_to_length’ function), at least on short strings:

```from timeit import Timer
t1 = Timer("repstr('foofoo', 30)", 'from __main__ import repstr')
t2 = Timer("repeat_to_length('foofoo', 30)", 'from __main__ import repeat_to_length')
t1.timeit()  # gives ~0.35 secs
t2.timeit()  # gives ~0.43 secs
```

Presumably if the string was long, or length was very high (that is, if the wastefulness of the string * length part was high) then it would perform poorly. And in fact we can modify the above to verify this:

```from timeit import Timer
t1 = Timer("repstr('foofoo' * 10, 3000)", 'from __main__ import repstr')
t2 = Timer("repeat_to_length('foofoo' * 10, 3000)", 'from __main__ import repeat_to_length')
t1.timeit()  # gives ~18.85 secs
t2.timeit()  # gives ~1.13 secs
```

Another FP aproach:

```def repeat_string(string_to_repeat, repetitions):
return ''.join([ string_to_repeat for n in range(repetitions)])
```

Not that there haven’t been enough answers to this question, but there is a repeat function; just need to make a list of and then join the output:

```from itertools import repeat
def rep(s,n):
''.join(list(repeat(s,n))
```

i use this:

```def extend_string(s, l):
return (s*l)[:l]
```

This is pretty pythonic:

```newstring = 'abc'*5
print newstring[0:6]
```