Python program to repeat M characters of a string N times

Here, we will figure out how to rehash a given number of characters (M) of a string N (given a number) times utilizing python program?

Given a string and we need to rehash it’s M characters N times utilizing python program.

Question:

Here we are furnished with a string and a non-negative whole number N, here we would think about that the front of the string is first M characters, or whatever is there in the string if the string length is not as much as M. Presently our assignment is to return N duplicates of the front. Likewise, think about these cases,

Example:

    mult_times('JustTech', 3, 2) = 'JusJus'
    mult_times('JustTechReview', 4, 3) = 'JustJustJust'
    mult_times ('Jus', 2, 3) = 'JuJuJu'

Arrangement:

Here we would first basically compose code for string esteem equivalent to M or less. As we are obscure for the estimation of N we would store our string an incentive in a variable and run a for circle for N times and each time we would store our incentive in that factor.

How about we comprehend this by Code, which would be more obvious,

Code:

def mult_times(str, m, n):
    front_len = m
    if front_len > len(str):
        front_len = len(str)
    front = str[:front_len]

    result = ''
    for i in range(n):
        result = result + front
    return result


print (mult_times('JustTechReview', 8, 5))
print (mult_times('kishan', 6, 3))
print (mult_times('Hello', 3, 7))

Output:

JustTechJustTechJustTechJustTechJustTech
kishankishankishan
HelHelHelHelHelHelHel

Leave a Comment