Write a Python program to get n (non-negative integer) copies of the first 2 characters of a given string. Return n copies of the whole string if the length is less than 2.

Program


def myfun(text,n):
    strlen = 2
    if strlen > len(text):
        strlen = len(text)
        
    substr = text[:strlen]
    result = ""
    for i in range(n):
        result = result + substr
    return result
      
if __name__=="__main__":      
    print(myfun('abcdef', 2)) 
    print(myfun('p', 3))  

Output:

abab
ppp

For latest job updates join Telegram Channel: https://t.me/sateeshm

Programs