-1

Is it possible to separate the string "a!b!" into two strings "a!" and "b!" and store that in a list? I have tried the split() function (and even with the delimiter "!"), but it doesn't seem to give me the right result that I want. Also, the character "!" could be any character.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
nii nii
  • 13
  • 2
  • @TDG I can use the re.split() function and just manipulate the results a bit to get the result that I want. Thanks! – nii nii Apr 16 '22 at 10:29

2 Answers2

0

split() is used when you need to seperate a string with particular character. If you want split a string into half, Try this

s = "a!b!"
l = [s[ : len(s)//2], s[len(s)//2 : ]]

# output : ["a!", "b!"]
Siva Reddy
  • 61
  • 6
0

How about :

string = 'a!ab!b!'
deliminator = '!'
word_list = [section+deliminator for section in string.split(deliminator) if section]
print(word_list)

Output :

['a!', 'ab!', 'b!']
Nilesh Bhave
  • 301
  • 1
  • 12