How to split a string on all punctuations in Python

2 Answers

0 votes
import re
 
string = 'python,java.c.php!javascript? : nodejs-c++.'
 
lst = list(filter(None, re.split('[ ,.!?:-]', string)))
 
print(lst)
 
 
 
'''
run:
 
['python', 'java', 'c', 'php', 'javascript', 'nodejs', 'c++']

'''

 



answered Aug 7, 2022 by avibootz
edited Aug 7, 2022 by avibootz
0 votes
import re
 
string = 'python,java.c.php!javascript? : nodejs-cpp.'
 
lst = "".join((ch if ch.isalpha() else " ") for ch in string).split()
 
print(lst)
 
 
 
'''
run:
 
['python', 'java', 'c', 'php', 'javascript', 'nodejs', 'cpp']

'''

 



answered Aug 7, 2022 by avibootz
...