Replace Forward Slash With Backslash 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.sub()).

Using replace()



Using translate() function



Using regular expression (re.sub())



To get more details about those concepts, continue reading on. There is more to learn. First of all, let’s discuss backslash.

Backslash in Python

In Python, the backslash is a special character.  First of all, it is used as part of a special character sequence; for example, “\n” means move to the next line, “\b” is the backspace character, and “\t” is tab space in Python code. In these cases, Python considers the sequence of characters as a single character in each case.

Secondly, a backslash can be used as an escape character – in this case, when a backslash is placed in front of a particular character, it changes the meaning of that character. In fact, backslash in Python is represented as “\\”.

Output: syntax error: unterminated string literal



In the first print statement, the backslash changes the meaning of the second quotation from being a closing quotation to being a literal string character hence the error “unterminated string literal”. Therefore, print(“example\”string”) will output example”string because the second quotation marks have been rendered literal string characters by the escape character -the backslash.

Once the concept of representing backslash in Python is clear, we can now discuss how to replace a forward slash with a backslash.

Method 1: Using the inbuilt String replace() function

The general syntax for replace() function is:

Where count is an optional argument representing the number of occurrences of the old_substring to be replaced. By default, the function replaces all occurrences of the given substring.



Note: replace() string returns a copy of the string after making the replacement, and therefore, the example_string variable will still have the original string even after execution.

Method 2: Using translate() function in Python

The translate() function allows one to replace one or multiple characters which should be provided in a dictionary as shown below. The code replaces forward-slash (/) and “e” in a string with a backslash.



Note that the keys for the translate dictionary must be a single character otherwise, you will run into an error.

Method 3: Using regular expression (re.sub()) in Python

As the name suggests, the pre-installed re package works with regular expressions to detect patterns in strings. The package has a function sub() that can be used to find and substitute substrings. For example,



In the re module, we need to pass “\\\\” as the pattern to capture a single backslash.

Reason: In the package, the backslash is also used as an escape character, and therefore, we need to pass “\\”, and since we also need “\\” for a Python literal string, then a valid pattern for a backslash is “\\\\”.

Alternatively, we can use raw string formatting (they are strings preceded by r), which converts backslash into a literal string. Note that, in the code, we still use “\\”, because the raw string only applies to the re pattern but we still need to write backslash as “\\”