Python Forum
Replace Single Backslash with Double Backslash in python
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace Single Backslash with Double Backslash in python
#1
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
Reply
#2
>>> 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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace a text/word in docx file using Python Devan 4 2,861 Oct-17-2023, 06:03 PM
Last Post: Devan
  python sql query single quote in a string mg24 1 996 Nov-18-2022, 08:01 PM
Last Post: deanhystad
  python Multithreading on single file mg24 3 1,672 Nov-05-2022, 01:33 PM
Last Post: snippsat
  change backslash into slash in a path paul18fr 7 2,179 Jul-21-2022, 04:26 PM
Last Post: deanhystad
  Setup Portable Python on Windows for script starts with double clicks? pstein 0 1,778 Feb-18-2022, 01:29 PM
Last Post: pstein
  python regex: get rid of double dot wardancer84 4 2,310 Sep-09-2021, 03:03 PM
Last Post: wardancer84
  Find and replace in files with regex and Python Melcu54 0 1,825 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 6,930 Mar-31-2021, 10:52 AM
Last Post: shantanu97
  Single digits seem to be greater than double digits Frosty_GM 4 3,436 Nov-20-2020, 10:13 AM
Last Post: DeaD_EyE
  Deny backslash using regex JohnnyCoffee 1 1,654 Mar-18-2020, 10:21 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020