Python – Split String on all punctuations
Given a String, Split the String on all the punctuations.
Input : test_str = ‘geeksforgeeks! is-best’
Output : [‘geeksforgeeks’, ‘!’, ‘is’, ‘-‘, ‘best’]
Explanation : Splits on ‘!’ and ‘-‘.Input : test_str = ‘geek-sfo, rgeeks! is-best’
Output : [‘geek’, ‘-‘, ‘sfo’, ‘, ‘, ‘rgeeks’, ‘!’, ‘is’, ‘-‘, ‘best’]
Explanation : Splits on ‘!’, ‘, ‘ and ‘-‘.
Method : Using regex + findall()
This is one way in which this problem can be solved. In this, we construct appropriate regex and task of segregating and split is done by findall().
Python3
# Python3 code to demonstrate working of # Split String on all punctuations # using regex + findall() import re # initializing string test_str = 'geeksforgeeks ! is-best, for @geeks' # printing original String print ( "The original string is : " + str (test_str)) # using findall() to get all regex matches. res = re.findall( r '\w+|[^\s\w]+' , test_str) # printing result print ( "The converted string : " + str (res)) |
Output
The original string is : geeksforgeeks! is-best, for @geeks The converted string : ['geeksforgeeks', '!', 'is', '-', 'best', ', ', 'for', '@', 'geeks']