# What is an efficient way to repeat a string to a certain length in Python?

One efficient way to repeat a string to a certain length in Python is to use the string multiplication operator (*) and string concatenation. Here are some code examples with step−by−step explanations:

## Using string multiplication operator

### Example

Define the original string and the desired length to repeat it to.

Calculate the number of times the string needs to be repeated by dividing the desired length by the length of the string using integer division (//).

Multiply the string by the calculated number of repetitions using the string multiplication operator (*).

Add any remaining characters to the end of the repeated string by slicing the original string from the beginning up to the remainder of the desired length divided by the length of the original string using modulo (%).

string = "foobar"
desired_length = 10
repeated_string = string * (desired_length // len(string)) + string[:desired_length % len(string)]
print(repeated_string)


### Output

foobarfoob


## Using while loop

### Example

Define the original string and the desired length to repeat it to.

Create an empty string variable to hold the repeated string.

Use a while loop to append the original string to the repeated string variable until its length is equal to or greater than the desired length.

Slice the repeated string to remove any extra characters beyond the desired length.

string = "lorem"
desired_length = 10

repeated_string = ""

while len(repeated_string) < desired_length:
repeated_string += string

repeated_string = repeated_string[:desired_length]

print(repeated_string)


### Output

loremlorem


## Using modulo and join function

### Example

Define the original string and the desired length to repeat it to.

Create a list comprehension that repeats the characters of the original string using modulo (%) to loop back to the beginning of the string when the end is reached.

Use the join() function to combine the characters in the list into a single string.

Assign the resulting string to the repeated_string variable.

string = "ipsum"
desired_length = 10

repeated_string = "".join([string[i % len(string)] for i in range(desired_length)])

print(repeated_string)


### Output

ipsumipsum


Here are few more code examples with step−by−step explanations for efficiently repeating a string to a certain length in Python:

## Using itertools cycle

### Example

Import the itertools module, which provides a cycle() function that repeats an iterable indefinitely.

Define the original string and the desired length to repeat it to.

Use itertools.cycle() to create an iterator that repeats the original string indefinitely.

Use itertools.islice() to limit the length of the iterator to the desired length.

Use "".join() to convert the sliced iterator into a string.

Assign the resulting string to the repeated_string variable.

import itertools
string = "Fooqux"
desired_length = 10
repeated_string = "".join(itertools.islice(itertools.cycle(string), desired_length))
print(repeated_string)


### Output

FooquxFooq


## Using string formatting

### Example

Define the original string and the desired length to repeat it to.

Use string multiplication (*) to repeat the original string by the number of times necessary to exceed the desired length, plus one.

Use string slicing ([:]) to trim the repeated string to the desired length.

Use string formatting to left−align (<) the trimmed string within a field of the desired length ({}).

Assign the resulting string to the repeated_string variable.

string = "Sayonara"
desired_length = 12
repeated_string = "{:<{}}".format((string * (desired_length // len(string) + 1))[:desired_length], desired_length)
print(repeated_string)


### Output

SayonaraSayo


## Using recursion

### Example

Define the original string and the desired length to repeat it to.

Define a function repeat_string() that takes two arguments: a string s and a desired length n.

If the length of the string s is greater than or equal to the desired length n, return a slice of s up to n.

Otherwise, recursively call repeat_string() with s concatenated with itself as the new string argument, and n as the same desired length argument.

Assign the result of the function call to the repeated_string variable.

Print the repeated_string variable to display the resulting string.

string = "Senorita"
desired_length = 15
def repeat_string(s, n):
if len(s) >= n:
return s[:n]
else:
return repeat_string(s + s, n)

repeated_string = repeat_string(string, desired_length)

print(repeated_string)



### Output

SenoritaSenorit


Updated on: 11-Aug-2023

193 Views