Python - Remove Punctuation from String Last Updated : 07 Jan, 2025 Comments Improve Suggest changes Like Article Like Report 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. Python s = "Hello, World! Python is amazing." # Create translation table import string translator = str.maketrans('', '', string.punctuation) # Remove punctuation clean_text = s.translate(translator) print(clean_text) OutputHello World Python is amazing Explanation:str.maketrans('', '', string.punctuation) creates a mapping to remove all punctuation.Thetranslate() method applies this mapping efficiently.The punctuation is stripped, leaving only the text.Let's explore some more ways and see how we can remove punctuation from string.Table of ContentUsing Regular Expressions (re.sub)Using List ComprehensionUsing filter()Using Regular Expressions (re.sub)This method is slightly less efficient but very flexible as it Works for complex patterns, not just punctuation. Python s = "Text cleaning? Regex-based! Works fine." # Removing punctuation import re clean = re.sub(r'[^\w\s]', '', s) print(clean) OutputText cleaning Regexbased Works fine Explanation:Regex Pattern: [^\w\s] matches any character that is not a word character or whitespace.re.sub() replaces matched punctuation with an empty string.Using List ComprehensionA concise way to remove punctuation is by iterating through the string and including only alphanumeric characters and spaces. Python s = "Hello, World! Python is amazing." import string # Filter out punctuation clean = ''.join([char for char in s if char not in string.punctuation]) print(clean) OutputHello World Python is amazing Explanation:char.isalnum() Checks if the character is alphanumeric.char.isspace() retains spaces between words.' '.join(...)combines the filtered characters into a single string.Using filter()filter() function can also be used for removing punctuation, providing a clean and functional approach. Python s = "Filtering... Is it clean now?" # Removing punctuation import string clean = ''.join(filter(lambda x: x not in string.punctuation, s)) print(clean) OutputFiltering Is it clean now Explanation:string.punctuation used to identify punctuation characters.filter() retains only characters that are not in string.punctuation.' '.join(...)combines the filtered characters. Comment More infoAdvertise with us Next Article Python - Remove Punctuation from String manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads 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 - Remove Punctuation Tuples Sometimes, while working with Python tuples, we can have a problem in which we need to perform the removal of all the tuples which contain punctuation in tuples. This kind of problem can occur in data filtering applications. Let's discuss certain ways in which this task can be performed. Input : tes 8 min read Remove spaces from a string in Python Removing spaces from a string is a common task in Python that can be solved in multiple ways. For example, if we have a string like " g f g ", we might want the output to be "gfg" by removing all the spaces. Let's look at different methods to do so:Using replace() methodTo remove all spaces from a s 2 min read 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', '-', ' 3 min read Python | Removing Initial word from string During programming, sometimes, we can have such a problem in which it is required that the first word from the string has to be removed. These kinds of problems are common and one should be aware about the solution to such problems. Let's discuss certain ways in which this problem can be solved. Met 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 - Remove substring list from String Our task is to remove multiple substrings from a string in Python using various methods like string replace in a loop, regular expressions, list comprehensions, functools.reduce, and custom loops. For example, given the string "Hello world!" and substrings ["Hello", "ld"], we want to get " wor!" by 3 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 How to Remove a Substring in Python? In Python, removing a substring from a string can be achieved through various methods such as using replace() function, slicing, or regular expressions. Depending on your specific use case, you may want to remove all instances of a substring or just the first occurrence. Letâs explore different ways 2 min read Like