Removing Backslashes from a String in Python

Removing backslashes from a string in Python

Your code is saying to replace each instance of '\' with result. Have you tried changing it to result.replace("\\", "") ?

how to remove backslash from string in python

You need to escape the backslash as \\.

filename = input("copy the file path with it's name and extension and paste it here to Encrypt: ")
# say it's something like "c:\myfiles\test.txt"
filename_replace = filename.replace("\\"," ")
# becomes "c: myfiles test.txt"

You can read more about escape characters and string literals here:
https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals

Removing backslashes from string

a = "I don\'t know why I don\'t have the right answer"
b = a.strip("/")
print b
  1. Slash (/) and backslash (\) are not the same character. There are no slashes anywhere in the string, so what you're doing will have no effect.
  2. Even if you used a backslash, there are no backslashes in a anyway; \' in a non-raw string literal is just a ' character.
  3. strip only removes "the leading and trailing characters". Since you're trying to remove a character in the middle of the string, it won't help.

And maybe some meta-problems:

  1. The OP's example string actually had just backslashes, not backslash-escaped apostrophes, even though his question title said the latter. So you were solving a different problem. Which may be more the OP's fault than yours, but it's still not a good answer.
  2. The OP's code already did what you were trying to do. He obviously had some other problem that he couldn't explain. (Given that he stopped answering comments and deleted his question, my first bet would be that it was a silly typo somewhere…) So, even if you wrote your code correctly, it wouldn't have been that helpful.

At any rate, that's not too many to count.

Then again, the comment didn't say it was too many to count, just hard to count. Some people have trouble counting to four three. Even Kings of the Britons have to be reminded by their clerics how to do it.

How to remove backslash from string in Python?

This is due to \ being the escape sequence character in python so you have to do \\

str2 = str1.replace('\\', '')  

How do you remove backslashes and the word attached to the backslash in Python?

Python is somewhat hard to convince to just ignore unicode characters. Here is a somewhat hacky attempt:

l = ['Historical Notes 1996',
'\ue606',
'The Future of farms 2012',
'\ch889',
'\8uuuu',]


def not_unicode_or_backslash(x):
try:
x = x.encode('unicode-escape').decode()
finally:
return not x.startswith("\\")


[x for x in l if not_unicode_or_backslash(x)]

# Output: ['Historical Notes 1996', 'The Future of farms 2012']

The problem is that you can't check directly whether or not the string starts with a backslash since \ue606 is not considered as the 6-character string, but as a single unicode character. Because of this, it does not start with a backslash and for

[x for x in l if not x.startswith("\\")]

you get

['Historical Notes 1996', '\ue606', 'The Future of farms 2012']

Python: Removing backslash in string?

In Python 2.7.13, this code:

text = "We\'ve ... here\'s why ... That\'s how ... it\'s"
text = text.replace("\\", "")
print text

outputs We've ... here's why ... That's how ... it's

Are you using a different version of Python, or do you have a specific section of code to look at?

Edit:

I also wanted to mention that the apostrophe's have a backslash before them because it's an escape character. This essentially just means that you're telling the terminal that python is outputting to to interpret the apostrophe literally (in this situation), and to not handle it differently than any other character.

It's also worth noting that there are other good uses for backslashes (\n, or newline is one of the most useful imho).

For example:

text = "We\'ve ... here\'s why ...\nThat\'s how ... it\'s"
print text

outputs:

We've ... here's why ...
That's how ... it's

Not that the \n is interpreted as a request for the terminal to go to a newline before interpreting the rest of the string.



Related Topics



Leave a reply



Submit