How do I replace punctuation in a string in Python?

后端 未结 6 1088
长情又很酷
长情又很酷 2021-02-01 19:31

I would like to replace (and not remove) all punctuation characters by \" \" in a string in Python.

Is there something efficient of the following flavo

6条回答
  •  渐次进展
    2021-02-01 19:48

    Replace by ''?.

    What's the difference between translating all ; into '' and remove all ;?

    Here is to remove all ;:

    s = 'dsda;;dsd;sad'
    table = string.maketrans('','')
    string.translate(s, table, ';')
    

    And you can do your replacement with translate.

提交回复
热议问题