[英]Why is there empty quotation marks?
Given a string, return a string where for every char in the original, there are two chars.给定一个字符串,返回一个字符串,其中对于原始字符串中的每个字符,都有两个字符。
double_char('The')
→ 'TThhee'
double_char('The')
→ 'TThhee'
double_char('AAbb')
→ 'AAAAbbbb'
double_char('AAbb')
→ 'AAAAbbbb'
double_char('Hi-There')
→ 'HHii--TThheerree'
double_char('Hi-There')
→ 'HHii--TThheerree'
Solution:解决方案:
def double_char(str):
double_char = ""
for i in range(len(str)):
double_char += str[i]*2
return double_char
Hello, I'm new here, and it's my first time discovering this website and adding a question, so I'm sorry if it's a bit unclear.你好,我是新来的,第一次发现这个网站并添加问题,所以如果有点不清楚,我很抱歉。 Can someone please explain this one in detail?
有人可以详细解释一下吗? Like why it has empty quotation marks?
比如为什么它有空引号?
double_char = ""
Create an empty string, to be able to add characters to it. double_char = ""
创建一个空字符串,以便能够向其中添加字符。
for i in range(len(str)):
For every letter for i in range(len(str)):
对于每个字母
double_char += str[i]*2
Add this letter twice to the string double_char += str[i]*2
将此字母两次添加到字符串中
return double_char
Return the string 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.