简体   繁体   中英

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('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?

double_char = "" Create an empty string, to be able to add characters to it.

for i in range(len(str)): For every letter

double_char += str[i]*2 Add this letter twice to the string

return double_char Return the string

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM