How to Double Characters in Python

The challenge

Given a string, you have to return a string in which each character (case-sensitive) is repeated once.

double_char("String") ==> "SSttrriinngg"

double_char("Hello World") ==> "HHeelllloo  WWoorrlldd"

double_char("1234!_ ") ==> "11223344!!__  "

The solution in Python code

This can easily be done by looping through each character and appending it to a list, which we then join and return at the end:

def double_char(s):
    out = []
    for i in s:
        out.append(i+""+i)
    return "".join(out)

However, we could simplify this down to a single list comprehension:

def double_char(s):
    return "".join([i+''+i for i in s])

Or go one step further:

def double_char(s):
    return ''.join(c * 2 for c in s)

Test cases to validate our solution

test.assert_equals(double_char("String"),"SSttrriinngg")
test.assert_equals(double_char("Hello World"),"HHeelllloo  WWoorrlldd")
test.assert_equals(double_char("1234!_ "),"11223344!!__  ")