Python - Repeat String till K


Python is used all over the world by different people for different purposes such as web development, machine learning, data science and to perform different processes with automation. In this article we will learn how to increase the length of a string by repeating the same string again and again until we achieve our required length.

Different Methods to Repeat Strings

String Multiplication

In this method we simply achieve the desired length by multiplication operation. We will provide a specific value and the string will be multiplied to reach the specific number of characters. The syntax of this method is as follows:

def repeat_string(data, length): # The input of the string is given along with the value of number of characters in the final string
    whole_string = length // len(data)  # The number of times the whole string will be written is found
    extra_characters = length % len(data)    # The number of times some additional characters will be added to achieve the number of characters is found
    
    final_string = data * whole_string + data[:extra_characters] # A new string is created by repeating the whole string and adding some additional characters to achieve the required length
    
    return final_string

Example

The example of the above method will be as follows:

def repeat_string(data, length): # The input of the string is given along with the value of number of characters in the final string
    whole_string = length // len(data)  # The number of times the whole string will be written is found
    extra_characters = length % len(data)    # The number of times some additional characters will be added to achieve the number of characters is found
    
    final_string = data * whole_string + data[:extra_characters] # A new string is created by repeating the whole string and adding some additional characters to achieve the required length
    
    return final_string
old_string = "Example" # The input of the string is given
final_length = 25  # The desired number of characters are specified

new_string = repeat_string(old_string, final_length) # The function is repeat_string is run
print(new_string)

Output

The output of the above code will be as follows:

ExampleExampleExampleExam

While Loop

In this method the while loop will keep executing the string until the required length is achieved and the statement becomes true. The syntax of the above method will be as follows:

def whole_string(data, length): # The input of the string is given along with the value of number of characters in the final string
    final_string = "" # A new empty string is created which have the repeated string
    
    while len(final_string) < length: # The Condition is provided to the loop to achieve the desired length 
        final_string += data
    
    final_string = final_string[:length]  # Remove all the undesired elements and make the string of the required length
    
    return final_string

Example

The example of the above method is as follows:

def whole_string(data, length): # The input of the string is given along with the value of number of characters in the final string
    final_string = "" # A new empty string is created which have the repeated string
    
    while len(final_string) < length: # The Condition is provided to the loop to achieve the desired length 
        final_string += data
    
    final_string = final_string[:length]  # Remove all the undesired elements and make the string of the required length
    
    return final_string
old_string = "Example" # The input of the string is given
final_length = 25 # The desired length of the string is specified

new_string = whole_string(old_string, final_length) # The function whole_string is run 
print(new_string)  # The output of the desired length is displayed

Output

The output of the method will be as follows:

ExampleExampleExampleExam

Itertools

Itertools is used for efficient checking of each element in a string. We will use one of the functions of the itertools library to achieve the desired length of the string and to stop the replication of the string we will use to the slicing function once the desired length is achieved. The syntax of this method will be as follows:

import itertools  # Do not forget to import itertools or else error might occur

def whole_string(data, length): # The input of the string is given along with the desired length of the string
    final_string = ''.join(itertools.islice(itertools.cycle(data), length)) #The itertools.cycle will help to repeat the string and the itertools.slice will stop the repeatation once the desired length is achieved 
    return final_string

Example

The example of the above method will be as follows:

import itertools  # Do not forget to import itertools or else error might occur

def whole_string(data, length): # The input of the string is given along with the desired length of the string
    final_string = ''.join(itertools.islice(itertools.cycle(data), length)) #The itertools.cycle will help to repeat the string and the itertools.slice will stop the repeatation once the desired length is achieved 
    return final_string
old_string = "Example" # The input of the string is given
final_length = 25 # The desired length of the string is specified

new_string = whole_string(old_string, final_length) # The function whole_string is run
print(new_string)  # The output of the desired length is displayed

Output

The output of the above code will be as follows:

ExampleExampleExampleExam

List Comprehension

This method will check each element in the string and then it will create a new string in which the elements will keep repeating until the desired length is achieved. The syntax of this method is as follows:

def whole_string(data, length): # The input of the string is given along with the required length of the string
    final_string = [char for char in data for _ in range(length // len(data) + 1)] # Each element of the string is checked and then they are repeated until desired length is achieved
    updated_string = ''.join(final_string[:length]) # Once the desired length is achieved the elements are joined together into 1
    
    return updated_string

Example

The example of the above method is as follows:

def whole_string(data, length): # The input of the string is given along with the required length of the string
    final_string = [char for char in data for _ in range(length // len(data) + 1)] # Each element of the string is checked and then they are repeated until desired length is achieved
    updated_string = ''.join(final_string[:length]) # Once the desired length is achieved the elements are joined together into 1
    
    return updated_string
old_string = "Example"  #The input of the string is given
final_length = 25 # The desired length of the string is provided

new_string = whole_string(old_string, final_length) # The function whole_string is run
print(new_string) # The output having the desired length is displayed

Output

The output of the example will be as follows:

EEEExxxxaaaammmmpppplllle

Conclusion

In Python, it's customary to repeat a string until it reaches a certain length. In this article, we looked at four distinct approaches to do this: string multiplication, a while loop, the use of the itertools functions and list comprehension. Depending on your individual needs and coding preferences, you may choose from each option, which offers a solution to the issue.

When working with enormous strings or exceptionally lengthy desired lengths, it's important to keep in mind the performance implications because certain approaches could be more effective than others.

Updated on: 01-Aug-2023

60 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements