python how can i split string with no blank?

Submitted 3 years, 1 month ago
Ticket #362
Views 398
Language/Framework Python
Priority Low
Status Closed

input = 'a+b++b+++c++5+6+7++8+9++10'
string = input.split('+')
print(string)

when we run this code the output is ['a', 'b', '', 'b', '', '', 'c', '', '5', '6', '7', '', '8', '9', '', '10']

But i want to split the string with no blank like ['a', 'b', 'b', 'c', '5', '6', '7', '8', '9', '10']

Is there any function or method to remove blanks without using for loop like


Submitted on Feb 28, 21
add a comment

1 Answer

Verified

Use a regex

 

import re

txt = 'a+b++b+++c++5+6+7++8+9++10'
x = re.split("[+]{1,}", txt)

if x:
  print(x)

Submitted 3 years, 1 month ago

Thanks a lot.

- sunil 3 years, 1 month ago


Latest Blogs