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
  • 72,202
  • 12
  • 116
  • 152
7edubs7
  • 15
  • 5
  • 1
    [Works on my machine](https://ideone.com/BAhfXl). – Kevin Jul 05 '19 at 18:47
  • Possible duplicate of [how to replace back slash character with empty string in python](https://stackoverflow.com/questions/12618030/how-to-replace-back-slash-character-with-empty-string-in-python) – Bill the Lizard Jul 05 '19 at 18:48
  • The given solution works for me. – Addison Lynch Jul 05 '19 at 18:50
  • So weird. I'm copying and pasting your code and it doesn't work on my machine. I'm stumped. – 7edubs7 Jul 05 '19 at 18:51
  • I'm actually trying myself on `jupyter notebook`on windows, and it does not work @Kevin – M.K Jul 05 '19 at 19:03
  • 3
    *There are no actual backslashes in that string* - those are just part of the `repr()` of the string, inserted so that the single quotes are syntactically valid inside a single-quoted string. – jasonharper Jul 05 '19 at 19:07

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
  • 823
  • 10
  • 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,116
  • 2
  • 22
  • 43