Given a string, return a string where for every char in the original, there are two chars.
double_char('The')
→ 'TThhee'
double_char('AAbb')
→ 'AAAAbbbb'
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?
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