Split a string by backslash in python

Question:

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

 a = "1234"

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

Asked By: user3184086

||

Answers:

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"1234"
>>> 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 = '1234'
>>> print(a)
1☻♥♦
>>> print(repr(a))
'1x02x03x04'

>>> b = '1\2\3\4'
>>> print(b)
1234
>>> print(repr(b))
'1\2\3\4'
Answered By: Henry Keiter

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 = "1234" is interpreted as an octal number. If you prefix the string with r, you will get the intended result.

Answered By: Jonas Raedle

According to this answer:

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

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

>>> a = "1234"
>>> a.encode('string-escape').split("\x")
['1', '02', '03', '04']
Answered By: jparonson

‘r’ does the trick for me

var = 'abcd'  
var.split('r')
['a', 'b', 'c', 'd']
Answered By: Shyam Dwivedi

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 = "aabfnrtvcdefi"
print(split_str(str))    
Answered By: Dinesh Kumar
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.