string.punctuation in Python Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In Python, the string module is a pre-initialized string used as a string constant. One such constant is string.punctuation, which provides a predefined string containing all the characters commonly considered punctuation.What is string.punctuation?string.punctuation is a string constant containing the following characters:!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~This constant comes in handy when we need to identify or remove punctuation from text. It saves us the hassle of manually defining or remembering all punctuation characters. Python import string # Check if a character is punctuation char = "!" if char in string.punctuation: print(f"{char}") Output! Explanation:The string module is imported to access string.punctuation.The if char in string.punctuation statement checks whether the value of char ("!") exists in the string.punctuation string. If it does, the condition evaluates to True, and the corresponding message is printed.Table of ContentSyntax of string.punctuationExamples of string.punctuation1. Removing Punctuation from a String2. Counting Punctuation in a String3. Filtering Punctuation from a ListSyntax of string.punctuationstring.punctuationParameters:None. This is a predefined constant in the string module.Return Type:Type: str (Returns a string containing all standard punctuation characters).Examples of string.punctuation1. Removing Punctuation from a StringIf we have a text string and want to strip out punctuation, string.punctuation is used to make this simple: Python import string # Input string s = "Learn Python, with GFG!!!" # Remove punctuation clean_txt = ''.join(ch for ch in s if ch not in string.punctuation) print(clean_txt) OutputLearn Python with GFG Explanation:The code uses a generator expression to iterate through each character in the input string 's'. It includes only those characters that are not present in string.punctuation.join() method combines the filtered characters into a new string, effectively removing all punctuation from the input string.2. Counting Punctuation in a StringWe can also count how many punctuation marks appear in a string: Python import string # Input string s = "Wow! Amazing... Isn't it?" # Count punctuation marks count = sum(1 for ch in s if ch in string.punctuation) print(count) Output6 Explanation:The code iterates over each character in the input string 's' using a generator expression. For each character, it checks if the character is in string.punctuation.The sum() function accumulates 1 for every character in 's' that is found in string.punctuation, effectively counting the total number of punctuation marks in the string.3. Filtering Punctuation from a ListWhen working with lists, we might want to filter out punctuation: Python import string # List of characters c = ['a', ',', 'b', '!', '?'] # Filter punctuation non_punc = [ch for ch in c if ch not in string.punctuation] print(non_punc) Output['a', 'b'] Explanation:The list comprehension iterates through each character in the list and excludes any character found in string.punctuation.The filtered characters ('a' and 'b') that are not punctuation marks are stored in a new list, non_punc. Comment More infoAdvertise with us Next Article Python String Concatenation K Kanchan_Ray Follow Improve Article Tags : Python python-string Python-string-functions Practice Tags : python Similar Reads Python String Concatenation String concatenation in Python allows us to combine two or more strings into one. In this article, we will explore various methods for achieving this. The most simple way to concatenate strings in Python is by using the + operator.Using + OperatorUsing + operator allows us to concatenation or join s 3 min read Python String Interpolation String Interpolation is the process of substituting values of variables into placeholders in a string. Let's consider an example to understand it better, suppose you want to change the value of the string every time you print the string like you want to print "hello <name> welcome to geeks for 4 min read Multiline String in Python A sequence of characters is called a string. In Python, a string is a derived immutable data typeâonce defined, it cannot be altered. To change the strings, we can utilize Python functions like split, join, and replace.Python has multiple methods for defining strings. Single quotations (''), double 4 min read Python String - printable() In Python string.printable is a pre-initialized string constant that contains all characters that are considered printable. This includes digits, ASCII letters, punctuation, and whitespace characters.Let's understand with an example:Pythonimport string # to show the contents of string.printable prin 2 min read Add padding to a string in Python Padding a string involves adding extra characters such as spaces or specific symbols to adjust its length or align it in a visually appealing way. Letâs dive into the most efficient methods to add padding to a string in Python.Using Python f-stringsF-strings allow us to specify padding directly with 2 min read Collections.UserString in Python Strings are the arrays of bytes representing Unicode characters. However, Python does not support the character data type. A character is a string of length one. Example: Python3 # Python program to demonstrate # string # Creating a String # with single Quotes String1 = 'Welcome to the Geeks World' 2 min read Like