Python - Split String on all punctuations
Last Updated :
07 Apr, 2023
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))
OutputThe original string is : geeksforgeeks! is-best, for @geeks
The converted string : ['geeksforgeeks', '!', 'is', '-', 'best', ', ', 'for', '@', 'geeks']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method : Using replace() and split() methods
Python3
# Python3 code to demonstrate working of
# Split String on all punctuations
# initializing string
test_str = 'geeksforgeeks ! is-best, for @geeks'
# printing original String
print("The original string is : " + str(test_str))
# import string library function
import string
# Storing the sets of punctuation in variable result
result = string.punctuation
for i in test_str:
if i in result:
test_str=test_str.replace(i,"*"+i+"*")
res=test_str.split("*")
# printing result
print("The converted string : " + str(res))
OutputThe original string is : geeksforgeeks ! is-best, for @geeks
The converted string : ['geeksforgeeks ', '!', ' is', '-', 'best', ',', ' for ', '@', 'geeks']
Method #3: Using re.sub() method with lambda function
Steps:
- Import the required module 're' and initialize the input string "test_str" and print the original string "test_str".
- Apply the 're.sub()' method to add space before and after each punctuation symbol.
- Assign the result to the variable "res" and split the modified string into a list of words.
- Print the resulting list of words.
Python3
# Python3 code to demonstrate working of
# Split String on all punctuations
# Using re.sub() method with lambda function:
import re
# initializing string
test_str = 'geeksforgeeks ! is-best, for @geeks'
# printing original String
print("The original string is : " + str(test_str))
res = re.sub(r'(\W+)', lambda x: ' '+x.group(0)+' ', test_str).split()
# printing result
print("The converted string : " + str(res))
OutputThe original string is : geeksforgeeks ! is-best, for @geeks
The converted string : ['geeksforgeeks', '!', 'is', '-', 'best', ',', 'for', '@', 'geeks']
Time complexity: O(n), as 're.sub()' method has a time complexity of O(n) where n is the length of the input string "test_str"
Space complexity: O(n), where n is the length of the input string "test_str".
Similar Reads
Split and join a string in Python The goal here is to split a string into smaller parts based on a delimiter and then join those parts back together with a different delimiter. For example, given the string "Hello, how are you?", you might want to split it by spaces to get a list of individual words and then join them back together
3 min read
Python - Remove Punctuation from String In this article, we will explore various methods to Remove Punctuations from a string.Using str.translate() with str.maketrans()str.translate() method combined with is str.maketrans() one of the fastest ways to remove punctuation from a string because it works directly with string translation tables
2 min read
Python - Split a String by Custom Lengths Given a String, perform split of strings on the basis of custom lengths. Input : test_str = 'geeksforgeeks', cus_lens = [4, 3, 2, 3, 1] Output : ['geek', 'sfo', 'rg', 'eek', 's'] Explanation : Strings separated by custom lengths.Input : test_str = 'geeksforgeeks', cus_lens = [10, 3] Output : ['geeks
2 min read
Python - Reverse String except punctuations Given a string with punctuations, perform string reverse, leaving punctuations at their places. Input : test_str = 'geeks@#for&%%gee)ks' Output : skeeg@#rof&%%ske)eg Explanation : Whole string is reversed, except the punctuations. Input : test_str = 'geeks@#for&%%gee)ks' [ only substring
5 min read
Python | Splitting operators in String Sometimes we have a source string to have certain mathematical statement for computations and we need to split both the numbers and operators as a list of individual elements. Let's discuss certain ways in which this problem can be performed. Method #1 : Using re.split() This task can be solved usin
7 min read
Python program to Sort Strings by Punctuation count Given the Strings list, sort by punctuations count. Input : test_list = ["gfg@%^", "is", "Best!"] Output : ['is', 'Best!', 'gfg@%^'] Explanation : 0 < 1 < 3, sorted by punctuation count. Input : test_list = ["gfg@%^", "Best!"] Output : [ 'Best!', 'gfg@%^'] Explanation : 1 < 3, sorted by pun
3 min read
Python | Split flatten String List Sometimes, while working with Python Strings, we can have problem in which we need to perform the split of strings on a particular deliminator. In this, we might need to flatten this to a single String List. Let's discuss certain ways in which this task can be performed. Method #1 : Using list compr
7 min read
Python | Exceptional Split in String Sometimes, while working with Strings, we may need to perform the split operation. The straightforward split is easy. But sometimes, we may have a problem in which we need to perform split on certain characters but have exceptions. This discusses split on comma, with the exception that comma should
4 min read
Python | Replace punctuations with K Sometimes, while working with Python Strings, we have a problem in which we need to perform the replacement of punctuations in String with specific characters. This can have applications in many domains such as day-day programming. Let's discuss certain ways in which this task can be performed. Meth
7 min read
Python | Split given string into equal halves We are given a string, we need to split it into two halves. If the string has an odd length, the first half should be longer by one character.Using String Slicing String Slicing is the efficient approach which splits the string at the midpoint. If the string length is odd, the first half automatical
3 min read