首页 > 解决方案 > 为什么这段代码不会删除我在 Python 中的反斜杠?

问题描述

我有一个字符串格式的代码块。它的变量名 my_string 如下所示:

'<div class="section page-centered"><div><b style="font-size: 24px">About 
Us</b></div><div>At <a href="https://close.com/" class="postings- 
link">Close</a>, we\'re building the sales
communication platform of the future. With our roots as the very first 
sales CRM to include built-in calling, we\'re leading the industry toward 
eliminating manual processes and helping compa
nies to close more deals (faster). Since our founding in 2013, we\'ve 
grown to become a profitable, 100% globally distributed team of ~33 high- 
performing, happy people that are dedicated to b
uilding a product our customers love. </div>'

我想将除反斜杠之外的所有内容都保留在该块中。我在这里看到了很多问题和答案

new_string = my_string.replace("\\", "")

但我只是得到相同的输出。我在这里做错了什么?

标签: pythonstring-formatting

解决方案


反斜杠实际上是在转义单引号。如果我打印字符串,我会得到:

print('<div class ... </div>')

<div class="section page-centered"><div><b style="font-size: 24px">About Us</b></div><div>At <a href="https://close.com/" class="postings- link">Close</a>, we're building the salescommunication platform of the future. With our roots as the very first sales CRM to include built-in calling, we're leading the industry toward eliminating manual processes and helping companies to close more deals (faster). Since our founding in 2013, we've grown to become a profitable, 100% globally distributed team of ~33 high- performing, happy people that are dedicated to building a product our customers love. </div>


推荐阅读