python how can i split string with no blank?

Submitted 6 months, 3 weeks ago
Ticket #362
Views 130
Language 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 6 months, 3 weeks ago

Thanks a lot.

- sunil 6 months, 3 weeks ago


Wanna Post Ad?
Reach a large group of audience by posting an ad about your business here. For more details
Drop us a Message