Hướng dẫn dùng string.punctuation python

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In Python3, string.punctuation is a pre-initialized string used as string constant. In Python, string.punctuation will give the all sets of punctuation.

    Syntax : string.punctuation

    Parameters : Doesn’t take any parameter, since it’s not a function.

    Returns : Return all sets of punctuation.

    Note : Make sure to import string library function inorder to use string.punctuation

    Code #1 :

    import string 

    result = string.punctuation 

    print(result) 

    Output :

    !"#$%&'()*+, -./:;<=>?@[\]^_`{|}~
    

     
    Code #2 : Given code tests for punctuation.

    import string 

    Sentence = "Hey, Geeks !, How are you?"

    for i in Sentence:

        if i in string.punctuation:

            print("Punctuation: " + i)

    Output:

    Punctuation:,
    Punctuation: !
    Punctuation:,
    Punctuation: ?
    

    Many times while working with Python strings, we have a problem in which we need to remove certain characters from strings. This can have applications in data preprocessing in the Data Science domain and also in day-day programming. Let’s discuss certain ways in which we can perform this task using Python.

    Method 1: Remove Punctuation from a String with Translate

    The first two arguments for string.translate method is empty strings, and the third input is a Python list of the punctuation that should be removed. This instructs the Python method to eliminate punctuation from a string. This is one of the best ways to strip punctuation from a string.

    Python3

    import string

    test_str = 'Gfg, is best: for ! Geeks ;'

    test_str = test_str.translate

        (str.maketrans('', '', string.punctuation))

    print(test_str)

    Output:

    Gfg is best for  Geeks 

    Method 2: Remove Punctuation from a String with Python loop

    This is the brute way in which this task can be performed. In this, we check for the punctuations using a raw string that contain punctuations and then we construct a string removing those punctuations.

    Python3

    test_str = "Gfg, is best : for ! Geeks ;"

    print("The original string is : " + test_str)

    punc =

    for ele in test_str:

        if ele in punc:

            test_str = test_str.replace(ele, "")

    print("The string after punctuation filter : " + test_str)

    Output: 

    The original string is : Gfg, is best : for ! Geeks ;
    The string after punctuation filter : Gfg is best  for  Geeks 

    Method 3: Remove Punctuation from a String with regex 

    The part of replacing with punctuation can also be performed using regex. In this, we replace all punctuation with an empty string using a certain regex.

    Python3

    import re

    test_str = "Gfg, is best : for ! Geeks ;"

    print("The original string is : " + test_str)

    res = re.sub(r'[^\w\s]', '', test_str)

    print("The string after punctuation filter : " + res)

    Output : 

    The original string is : Gfg, is best : for ! Geeks ;
    The string after punctuation filter : Gfg is best  for  Geeks 

    Method 4:  Using for loop, punctuation string and not in operator

    Python3

    test_str = "Gfg, is best : for ! Geeks ;"

    print("The original string is : " + test_str)

    punc =

    res=" "

    for ele in test_str:

        if ele not in punc:

            res+=ele

    print("The string after punctuation filter : " + res)

    Output

    The original string is : Gfg, is best : for ! Geeks ;
    The string after punctuation filter :  Gfg is best  for  Geeks 

    The Time and Space Complexity for all the methods are the same:

    Time Complexity: O(n)

    Auxiliary Space: O(n)