How to split part of a string by hyphen using regex and maxsplit in Python

2 Answers

0 votes
import re
 
s = 'Python-Java-PHP-C#-C++'
 
sp = re.split('-', s, 2)
 
print(sp)
 
  
'''
run:
  
['Python', 'Java', 'PHP-C#-C++']
  
'''

 



answered Jul 31, 2019 by avibootz
0 votes
import re
 
s = 'Python-Java-PHP-C#-C++-C'
 
sp = re.split('-', s, 3)
 
print(sp)
 
  
'''
run:
  
['Python', 'Java', 'PHP', 'C#-C++-C']
  
'''

 



answered Jul 31, 2019 by avibootz

Related questions

1 answer 158 views
1 answer 205 views
1 answer 177 views
1 answer 107 views
...