How to replace a double backslash with a single backslash in python? [duplicate]
Python3: >>> b’\\u201c’.decode(‘unicode_escape’) ‘“’ or >>> ‘\\u201c’.encode().decode(‘unicode_escape’) ‘“’
Python3: >>> b’\\u201c’.decode(‘unicode_escape’) ‘“’ or >>> ‘\\u201c’.encode().decode(‘unicode_escape’) ‘“’
Got stumped by this for ages and all the answers kept insisting that the source string needs to already have escaped backslashes in it … which isn’t always the case. Do this .. var replaced = str.replace(String.fromCharCode(92),String.fromCharCode(92,92));
(See ES2015 update at the end of the answer.) You’ve tagged your question both string and regex. In JavaScript, the backslash has special meaning both in string literals and in regular expressions. If you want an actual backslash in the string or regex, you have to write two: \\. The following string starts with one … Read more
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’ >>> … Read more
Do it like that: a, b, c, d = range(1, 5) result = ( # First is 1 a * # Then goes 2, result is 2 now b * # And then 3, result is 6 c * # And 4, result should be 24 d ) Actually, according to PEP8 parentheses are preferred … Read more
The backslash before the semicolon is used, because ; is one of list operators (or &&, ||) for separating shell commands. In example: command1; command2 The find utility is using ; or + to terminate the shell commands invoked by -exec. So to avoid special shell characters from interpretation, they need to be escaped with … Read more
Windows is the bastard child of operating systems in this regard, but a lot of APIs will accept forward slashes as well. On Windows, a file path looks like this: C:\Users\jsmith\Documents\file.txt On a Unix-like system (including Mac OS X and Linux), the same path would look like this: /home/jsmith/Documents/file.txt A URL, standardized in RFC 1738, … Read more
http://locutus.io/php/strings/addslashes/ function addslashes( str ) { return (str + ”).replace(/[\\”‘]/g, ‘\\$&’).replace(/\u0000/g, ‘\\0’); }
What you are seeing is the representation of my_string created by its __repr__() method. If you print it, you can see that you’ve actually got single backslashes, just as you intended: >>> print(my_string) why\does\it\happen? The string below has three characters in it, not four: >>> ‘a\\b’ ‘a\\b’ >>> len(‘a\\b’) 3 You can get the standard … Read more
replace all the \ with \\. it’s trying to escape the next character in this case the U so to insert a \ you need to insert an escaped \ which is \\