How to Replace Back Slash Character with Empty String in Python

how to replace back slash character with empty string in python

We need to specify that we want to replace a string that contains a single backslash. We cannot write that as "\", because the backslash is escaping the intended closing double-quote. We also cannot use a raw string literal for this: r"\" does not work.

Instead, we simply escape the backslash using another backslash:

result = string.replace("\\","")

How to replace backslash followed by 2 letters with empty string in Python?

I think you are trying to replace the unicode character '\xa0' -

s = 'FF 6 VRV AVENUE SUBRAMANIYAM PALAYAM PinCode:-\xa0641034' 
s = s.replace('\xa0', '')
print(s)
#'FF 6 VRV AVENUE SUBRAMANIYAM PALAYAM PinCode:-641034'

Want to replace backslash in python3 string

You can use the following positive lookahead assertion '\\(?=")':

import re

my_str = "\"abc INC\\\",\"None\", \"0\", \"test\""
p = re.sub(r'\\(?=")', '|', my_str)
print(p)
# '"abc INC|","None", "0", "test"'

Try not to use builtin names as names for variables, viz. str, to avoid shadowing the builtin.

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

python unicode replace backslash u with an empty string

u'\u2014' is actually -. It's not a number. It's a utf-8 character. Try using print keyword to print it . You will know

This is the output in ipython:

In [4]: print("val = ", u'\u2014')
val = —

Based on your comment, here is what you are doing wrong
"-" is not same as "EM Dash" Unicode character(u'\u2014')

So, you should do the following

print(u'\u2014'.replace("\u2014",""))

and that will work

EDIT:
since you are using python 2.x, you have to encode it with utf-8 as follows

u'\u2014'.encode('utf-8').decode('utf-8').replace("-","")

Replacing everything with a backslash till next white space

Does that work for you?

re.sub(
r"\\\w+\s*", # a backslash followed by alphanumerics and optional spacing;
'', # replace it with an empty string;
input_string # in your input string
)

>>> re.sub(r"\\\w+\s*", "", r"\fs24 hello there")
'hello there'
>>> re.sub(r"\\\w+\s*", "", "hello there")
'hello there'
>>> re.sub(r"\\\w+\s*", "", r"\fs24hello there")
'there'
>>> re.sub(r"\\\w+\s*", "", r"\fs24hello \qc23424 there")
'there'

Use Python to replace forward slashes in string to backslashes

Two backslashes mean a literal backslash:

s.replace("/", "\\")

python replace single backslash with double backslash

No need to use str.replace or string.replace here, just convert that string to a raw string:

>>> strs = r"C:\Users\Josh\Desktop\20130216"
^
|
notice the 'r'

Below is the repr version of the above string, that's why you're seeing \\ here.
But, in fact the actual string contains just '\' not \\.

>>> strs
'C:\\Users\\Josh\\Desktop\\20130216'

>>> s = r"f\o"
>>> s #repr representation
'f\\o'
>>> len(s) #length is 3, as there's only one `'\'`
3

But when you're going to print this string you'll not get '\\' in the output.

>>> print strs
C:\Users\Josh\Desktop\20130216

If you want the string to show '\\' during print then use str.replace:

>>> new_strs = strs.replace('\\','\\\\')
>>> print new_strs
C:\\Users\\Josh\\Desktop\\20130216

repr version will now show \\\\:

>>> new_strs
'C:\\\\Users\\\\Josh\\\\Desktop\\\\20130216'

how to replace single backslash with empty character in string?

Back Slash itself is an escape Character,so you need to use double backslash to achieve it,as in

message=message.replaceAll("\\","");


Related Topics



Leave a reply



Submit