on Python, given a word for example "peter" and any emoji for example "💛" how can I get this result

Question:

on Python, given a word for example "peter" and any emoji for example " "
how can I get this result

`word = "Peter"
emoji = " "

# Print the word with doubled letters followed by the emoji
print(''.join([l * 2 for l in word]) + emoji)

# Loop through the positions of the emoji and construct two strings for each position
for i in range(1, len(word) * 2, 2):
    first = ''.join([word[j] * 2 for j in range(i//2+1)])
    second = ''.join([word[j] * 2 for j in range(i//2+1, len(word))])
    print(first + emoji + second)
    print(first + second + emoji)

# Print the emoji followed by the reversed word with doubled letters, without the last two characters
print(emoji + ''.join([l * 2 for l in word[::-1][2:]]))`

I am not having this

enter image description here

Asked By: Sophie Tejada

||

Answers:

As I described. I convert the string to a list so I can use insert to insert the emoji into place. You could do this with string operations if you want.

word = "Peter"
emoji = " "

# Print with doubled letters.
def double(s):
    o = [l*2 for l in s]
    return ' '.join(o)

for i in range(len(word),-1,-1):
    wordl = list(word)
    wordl.insert(i,emoji)
    print(double(wordl))

Output:

PP ee tt ee rr  
PP ee tt ee   rr
PP ee tt   ee rr
PP ee   tt ee rr
PP   ee tt ee rr
  PP ee tt ee rr
Answered By: Tim Roberts
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.