首页 > 技术文章 > 【KataDaily 20220105】String repeat(字符重复)

bcaixl 2022-01-05 22:18 原文

题目:

Description:
Write a function called repeatStr which repeats the given string string exactly n times.
repeatStr(6, "I") // "IIIIII"
repeatStr(5, "Hello") // "HelloHelloHelloHelloHello"

 

自答:

def repeat_str(repeat, string):
    return "".join([string for i in range(repeat)])

 

高赞回答:

def repeat_str(repeat, string):
    return repeat * string

 

 

推荐阅读