35

Simple question but I'm struggling with it for too much time. Basically I want to split a string by \ (backslash).

 a = "1\2\3\4"

Tried to escape the the backslash but it doesn't seem to work:

 print(a.split('\'))
 print(a.split('"\"'))
 print(a.split('\\'))
 print(a.split('"\\"'))

I want to get this result:

 ['1','2','3','4']

Many thanks in advance

user3184086
  • 711
  • 1
  • 9
  • 16
  • If you're creating your string exactly as shown (`a = "1\2\3\4"`), that rather than your `split` call is the problem. Escape the \ in your string declaration or use `r""`. – Peter DeGlopper Jul 30 '14 at 22:25

6 Answers6

59

You have the right idea with escaping the backslashes, but despite how it looks, your input string doesn't actually have any backslashes in it. You need to escape them in the input, too!

>>> a = "1\\2\\3\\4"  # Note the doubled backslashes here!
>>> print(a.split('\\'))  # Split on '\\'
['1', '2', '3', '4']

You could also use a raw string literal for the input, if it's likely to have many backslashes. This notation is much cleaner to look at (IMO), but it does have some limitations: read the docs!

>>> a = r"1\2\3\4"
>>> print(a.split('\\'))
['1', '2', '3', '4']

If you're getting a elsewhere, and a.split('\\') doesn't appropriately split on the visible backslashes, that means you've got something else in there instead of real backslashes. Try print(repr(a)) to see what the "literal" string actually looks like.

>>> a = '1\2\3\4'
>>> print(a)
1☻♥♦
>>> print(repr(a))
'1\x02\x03\x04'

>>> b = '1\\2\\3\\4'
>>> print(b)
1\2\3\4
>>> print(repr(b))
'1\\2\\3\\4'
Henry Keiter
  • 16,863
  • 7
  • 51
  • 80
  • what if the a variable is not an input as in my question but the result of a function. Imagine like: a = function() ; print a results in a="1\2\3\4". Is there a way to convert the string to a raw string? – user3184086 Jul 30 '14 at 22:30
  • @user3184086 If `print(a)` results in `1\2\3\4`, then you do indeed have backslashes, and splitting on `'\\'` *will* split on them. If it's not, that means you've got some other character in there: try `print(repr(a))` to see a literal representation of `a`, escape characters and all. – Henry Keiter Jul 30 '14 at 22:36
  • "If ... a.split('\\') doesn't appropriately split on the visible backslashes, that means you've got something else in there instead of real backslashes. Try print(repr(a)) to see what the "literal" string actually looks like." yasss – abe Jul 12 '17 at 19:45
5

You can split a string by backslash using a.split('\\').

The reason this is not working in your case is that \x in your assignment a = "1\2\3\4" is interpreted as an octal number. If you prefix the string with r, you will get the intended result.

Jonas Raedle
  • 118
  • 1
  • 1
  • 6
3

According to this answer:

https://stackoverflow.com/a/2081708/3893465

you'll need to escape the backslashes before splitting as such:

>>> a = "1\2\3\4"
>>> a.encode('string-escape').split("\\x")
['1', '02', '03', '04']
Community
  • 1
  • 1
jparonson
  • 322
  • 1
  • 4
2

'\r' does the trick for me

var = 'a\b\c\d'  
var.split('\r')
['a', 'b', 'c', 'd']
Siong Thye Goh
  • 3,518
  • 10
  • 23
  • 31
1

Covering all the special cases like \a,\b, \f, \n, \r, \t, \v (String Literals)

def split_str(str):
    ref_dict = {
        '\x07':'a',
        '\x08':'b',
        '\x0C':'f',
        '\n':'n',
        '\r':'r',
        '\t':'t',
        '\x0b':'v',                
    }    
    res_arr = []
    temp = ''
    for i in str :
        if not i == '\\':
            if i in ref_dict:
                if not temp == "":
                    res_arr.append(temp)
                res_arr.append(ref_dict[i])
                temp = ''
            else:    
                temp += i
        else:
            if not temp == '':
                res_arr.append(temp)
            temp = ''
    res_arr.append(temp)
    return res_arr


str = "a\a\b\f\n\r\t\v\c\d\e\f\i"
print(split_str(str))    
Dinesh Kumar
  • 478
  • 4
  • 16
0

You could just replace the backslashes with a pipe (|) and go on with your program.

All text editors have a replace character option, where you could replace every occurence of \ with "|".

This is my text file containing words separated by backslashes

Step 1

Step 2

Step 3

Here, the first box will give you the option to input the recurring character that you wish to replace. The first box is the 'find character' field, and the second box is the 'replace character' field.

Below is our desired output (formatted file) !

This is the required code to input the strings in the form of a list after using the above mentioned method

Here is the list of strings that you can now use in your program !

The same method can be used on Windows, Linux, ect. as well !

LeNan18
  • 183
  • 1
  • 10