How to remove specific substrings from a set of strings in Python?

后端 未结 10 1920
自闭症患者
自闭症患者 2020-12-07 12:08

I have a set of strings set1, and all the strings in set1 have a two specific substrings which I don\'t need and want to remove.
Sample Input

10条回答
  •  天命终不由人
    2020-12-07 12:21

    >>> x = 'Pear.good'
    >>> y = x.replace('.good','')
    >>> y
    'Pear'
    >>> x
    'Pear.good'
    

    .replace doesn't change the string, it returns a copy of the string with the replacement. You can't change the string directly because strings are immutable.

    You need to take the return values from x.replace and put them in a new set.

提交回复
热议问题