![](/img/trans.png)
[英]How can I remove all words that end in “:” from a string in Python?
[英]How can I remove a phrase from a string but not the individual words
已經解決了,那是我的注意力不集中。 report = report.replace('contact sales', '')
有效,因為replace
返回新字符串並且不改變原始字符串。
我想刪除一些
聯系銷售
來自字符串的實例,但我不想刪除
接觸
要么
銷售量
如果它們單獨存在。
我試過: report.replace('contact sales', '')
但它沒有用。 它會檢測到該短語,但不會將其刪除。 我如何完成這項工作?
您可以通過在替換實例時重新定義變量來完成此操作。
例如:
report = report.replace('contact sales', '')
其背后的邏輯是 replace() function 不會就地修改字符串,它會使用替換的實例創建一個新字符串。 這就是為什么你需要重新定義它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.