How do you replace one backslash in python?

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'

Thread Rating:

  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Replace Single Backslash with Double Backslash in python

Mar-19-2017, 07:56 AM (This post was last modified: Mar-19-2017, 07:56 AM by saswatidas437.)

I have a need to replace single backslashes with double backslashes. I know that the replace method of the string object can be used, however I'm not able to figure out the correct combination given that the backslash serves as an escape character. Adding one more point below string is not constant below string i will read from a config file so i can't use python raw string r'\\[string]\[string]\[string]'

\\[string]\[string]\[string]
Needs to become
\\\\[string]\\[string]\\[string]

Example \\user\build\field to \\\\user\\build\\field

Posts: 4,231

Threads: 97

Joined: Sep 2016

Reputation: 273

>>> single = r'\folder\file'
>>> print(single)
\folder\file
>>> double = single.replace('\\', '\\\\')
>>> print(double)
\\folder\\file

Basically, any time you need to indicate a single backslash, you use a double backslash.

Posts: 687

Threads: 37

Joined: Sep 2016

Reputation: 24

See below. Note the difference between the interactive interpreter printing the results (uses repr()) and the output of explicit print statements (that use str()).

Output:

>>> x="a\\b\\c" >>> x 'a\\b\\c' >>> print x a\b\c >>> x.replace('\\','\\\\') 'a\\\\b\\\\c' >>> print x.replace('\\','\\\\') a\\b\\c >>>

But... doing this is seldom required, and when it is (escaping string in regular expressions, for instance), this alone is not sufficient, and there is usually a function that does a complete job (usually called quote() or escape()).

Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net

Possibly Related Threads…
Thread Author Replies Views Last Post
  change backslash into slash in a path paul18fr 7 398 Jul-21-2022, 04:26 PM
Last Post: deanhystad
  Setup Portable Python on Windows for script starts with double clicks? pstein 0 618 Feb-18-2022, 01:29 PM
Last Post: pstein
  python regex: get rid of double dot wardancer84 4 1,298 Sep-09-2021, 03:03 PM
Last Post: wardancer84
  Find and replace in files with regex and Python Melcu54 0 1,248 Jun-03-2021, 09:33 AM
Last Post: Melcu54
  Remove single and double quotes from a csv file in 3 to 4 column shantanu97 0 5,095 Mar-31-2021, 10:52 AM
Last Post: shantanu97
  Single digits seem to be greater than double digits Frosty_GM 4 1,893 Nov-20-2020, 10:13 AM
Last Post: DeaD_EyE
  Deny backslash using regex JohnnyCoffee 1 1,111 Mar-18-2020, 10:21 PM
Last Post: snippsat
  Cannot Remove the Double Quotes on a Certain Word (String) Python BeautifulSoup soothsayerpg 5 5,558 Oct-27-2019, 09:53 AM
Last Post: newbieAuggie2019
  merging sublist into single list in python abhishek8singhai 8 6,567 Mar-22-2019, 11:46 PM
Last Post: micseydel
  Replace characters from file in Python 2.7 melmouja 2 1,844 Feb-04-2019, 01:32 PM
Last Post: melmouja

How do I find and replace backslash in Python?

To replace backslashes in a string with Python, the easiest way is to use the Python built-in string replace() function. string_with_backslashes = "This/is/a/string/with/backslashes." string_with_underscores = string_with_backslashes.

How do you replace a backslash in a string in Python?

A forward-slash (/) in a Python string can be replaced by a backslash (\) using the String replace() function, translate() method, or regular expression(re..
1 Using replace().
2 Using translate() function..
3 Using regular expression (re.sub()).
4 Backslash in Python..

How do you remove a single backslash from a string in Python?

Using replace() Function to Remove Backslash from String in Python. The replace() can be utilized to remove an escape sequence or just a backslash in Python by simply substituting it with space in Python.

How do you replace a backslash?

To replace all backslashes in a string: Call the replaceAll() method, passing it a string containing two backslashes as the first parameter and the replacement string as the second. The replaceAll method will return a new string with all backslashes replaced by the provided replacement.