user11717179
user11717179

Reputation:

Multiple specified separator in split() Python

How to specify multiple separators in split() in python?

input = "print{hello world};x = 2 + 3;"
x = input.split('{') #this works but not the desired OP
x = input.split('{','}') #error 
x = input.split('{}') #this works but not the desired OP

Desired OP:

['print', 'hello world', ';x = 2 + 3;']

Upvotes: 4

Views: 103

Answers (1)

kederrac
kederrac

Reputation: 17322

you could use re.split in this way:

import re

my_input = "print{hello world};x = 2 + 3;"
re.split('{|}', my_input)

output:

['print', 'hello world', ';x = 2 + 3;']

Upvotes: 4

Related Questions