python: padding punctuation with white spaces (keeping punctuation)

前端 未结 3 1028
青春惊慌失措
青春惊慌失措 2021-02-05 19:16

What is an efficient way to pad punctuation with whitespace?

input:

s = \'bla. bla? bla.bla! bla...\'

desired output:

          


        
3条回答
  •  感动是毒
    2021-02-05 20:09

    If you use python3, use the maketrans() function.

    import string   
    text = text.translate(str.maketrans({key: " {0} ".format(key) for key in string.punctuation}))
    

提交回复
热议问题