0

I have a block of code that is in the format of a string. It's got a variable name of my_string looks like this:

'<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>'

I want to keep everything in that block except the backslashes. I've seen many questions and answers here where the solution would be

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

But I just get the same output. What am I doing wrong here?

Kevin
  • 74,910
  • 12
  • 133
  • 166
7edubs7
  • 15
  • 6

2 Answers2

3

The backslashes are actually escaping the single quotes. If I print the string, I get:

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>

Alex
  • 963
  • 9
  • 11
0

I am not exactly sure why it is working on some machines but, in mine at least, there is this problem:

  • A backlash followed by something is not really a backlash unless it is a double backlash:

as

back_lash="hi\myname\is\backlash"
print(back_lash)

This will output:

hi\myname\iacklash

It is easier to see in an IDE that can colour that.

Example

So as you can see, it is quite a special character. The same thing you did in your replace with a double backlash

As told in here, its difficult the backlash scape. The backslash ("\") character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character. you can use the prefix r or R:

String literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and use different rules for interpreting backslash escape sequences.

r"Some string with \ backslash"

Take a look at this, they mention:

You need to escape your backslash by preceding it with, yes, another backslashThe \ character is called an escape character, which interprets the character following it differently. For example, n by itself is simply a letter, but when you precede it with a backslash, it becomes \n, which is the newline character.

As you can probably guess, \ also needs to be escaped so it doesn't function like an escape character. You have to... escape the escape, essentially.

M.K
  • 1,464
  • 2
  • 24
  • 46