Python字符串具有许多有用的方法。
一种这样的方法是替换方法。
使用此方法,我们可以用另一个字符或者子字符串替换特定字符或者子字符串。
让我们来看看一个例子。
s = "Hello World, Welcome to my blog." print(s) s1 = s.replace('W', 'V') print(s1)
删除标点符号:
user_comment = "NGL, i just loved the moviee...... excellent work !!!" print(f"input string: {user_comment}") clean_comment = user_comment #copy the string in new variable, we'll store the result in this variable # define list of punctuation to be removed punctuation = ['.','.','!'] # iteratively remove all occurrences of each punctuation in the input for p in punctuation: clean_comment = clean_comment.replace(p,'') #not specifying 3rd param, since we want to remove all occurrences print(f"clean string: {clean_comment}")
日期:2020-07-15 11:16:26 来源:oir作者:oir