给定字符串,返回一个字符串,其中原始字符串中的每个字符都有两个字符。
double_char('The')
→'TThhee'
double_char('AAbb')
→'AAAAbbbb'
double_char('Hi-There')
→'HHii--TThheerree'
解决方案:
def double_char(str):
double_char = ""
for i in range(len(str)):
double_char += str[i]*2
return double_char
你好,我是新来的,这是我第一次发现这个网站并添加问题,所以如果有点不清楚,我很抱歉。有人能详细解释一下这个吗?比如为什么它有空引号?
def double_char(str): # function double_char takes argument str
double_char = "" # create an empty string assign to variable double_char. In python you put "" or '' around text that you want as a string (text).
for i in range(len(str)): # iterate from 0 to length of string
double_char += str[i]*2 # append character at position i from str and duplicate it
return double_char # return double_char
double_char = ""
创建一个空字符串,以便能够向其中添加字符。
for i in range(len(str)):
每个字母
double_char += str[i]*2
将此字母两次添加到字符串
return double_char
返回字符串