string.punctuation in Python
In this tutorial, we will learn about the string.punctuation in Python. We will also see an example code to understand the use of this operation.
What is string.punctuation in Python
string.punctuation is a pre-initialized string, therefore, it can be used as a string constant. In python, string. punctuation will give all sets of punctuation.
Also, note that it is not a function therefore there will be no parameters taken.
Since string.punctuation in Python is an operation of string, therefore, string module needs to be imported as :
import string print(string.punctuation)
The output of the following line of code will give a constant set of punctuations as output :
A code for removing all punctuations from a given sentence:
import string a = input("Enter string to be checked") print("The string without any punctuations is ", end=" ") # end used to print the output in one line for i in a: if i in string.punctuation: # checking for occurence of any punctuation i = "" print(i, end="")
Enter string to be checked Wow! What,.$#a [email protected]&(day*)+-
The string without any punctuations is Wow Whata wonderfulday
- Import string module to use its methods and functions.
- Enter the string for checking.
- Use in keyword to check for punctuation in the string entered.
- Use string.punctuation for checking.
- If present, the punctuation will be replaced by a blank.
You can also read about: