Python Forum

Full Version: Doubling every second letter.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello!
I have to create a program, which will be doubles every second letter. Foe example:
input:Tom has cat.
output: Toom hhass caat.
I wrote this cod :
def double(s):
    return ''.join(x * (y% 2 + 1)
    for y, x in enumerate(s))

s=input("text:")
print(double(s))
I have problem, because this program counts whitespace.How to fix it?
One way is to use slice to deduct count of whitespaces from index.

Below it presented as one-liner, but I think that conventional nested representation will make it more readable.

>>> a = 'Tom has cat'
>>> ''.join([v if v == ' ' or (i - a[:i+1].count(' ')) % 2 == 0 else v * 2 for i, v in enumerate(a)])
Toom hhass caat