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
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
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
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