Python: Create strikethrough / strikeout / overstrike string type

后端 未结 3 1632
灰色年华
灰色年华 2021-01-02 21:46

I would appreciate some help in creating a function that iterates through a string and combines each character with a strikethrough character (\\u0336). With the output bein

3条回答
  •  醉梦人生
    2021-01-02 22:25

    Edited

    As pointed out by roippi other answers so far are actually correct, and this one below is wrong. Leaving it here in case others get the same wrong idea that I did.


    Other answers so far are wrong - they do not strike out the first character of the string. 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.

提交回复
热议问题