Python: create string style strickethrough / outout / overstrike

I would appreciate help creating a function that iterates through a string and concatenates each strikethrough character (\ u0336). In this case, the output is an aligned version of the original line. Like this. ...

Something like.

def strike(text):
    i = 0
    new_text = ''
    while i < len(text):
        new_text = new_text + (text[i] + u'\u0336')
        i = i + 1
    return(new_text)

      

So far I have managed to unite, not unite.

+3


source to share


3 answers


def strike(text):
    result = ''
    for c in text:
        result = result + c + '\u0336'
    return result

      



Cool effect.

+5


source


What about:

from itertools import repeat, chain

''.join(chain.from_iterable(zip(text, repeat('\u0336'))))

      



or even simpler,

'\u0336'.join(text) + '\u0336'

      

+10


source


Edited

As noted in roippi , the other answers are actually correct and that below is wrong. Leaving it here in case others get the same wrong idea as me.


The other answers are still wrong - they don't cross out the first character of the line. Try this instead:

def strike(text):
    return ''.join([u'\u0336{}'.format(c) for c in text])

>>> print(strike('this should do the trick'))
'̶t̶h̶i̶s̶ ̶s̶h̶o̶u̶l̶d̶ ̶d̶o̶ ̶t̶h̶e̶ ̶t̶r̶i̶c̶k'

      

This will work in Python 2 and Python 3.

+3


source







All Articles