How to split a string by one or more adjacent newlines in Python

2 Answers

0 votes
import re

s = '''python



java



c++


c'''

s = re.split('\n+', s)
 
print(s)
 
 
 
'''
run:
 
['python', 'java', 'c++', 'c']
 
'''

 



answered Oct 26, 2020 by avibootz
0 votes
import re

s = 'python\n\n\njava\n\nc++\n\n\n\nc'

s = re.split('\n+', s)
 
print(s)
 
 
 
'''
run:
 
['python', 'java', 'c++', 'c']
 
'''

 



answered Oct 26, 2020 by avibootz
...