Python: Create strikethrough / strikeout / overstrike string type

后端 未结 3 1637
灰色年华
灰色年华 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:21

    How about:

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

    or even more simply,

    '\u0336'.join(text) + '\u0336'
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-02 22:33
    def strike(text):
        result = ''
        for c in text:
            result = result + c + '\u0336'
        return result
    

    Cool effect.

    0 讨论(0)
提交回复
热议问题