Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

remove punctuation from string python

#with re
import re
s = "string. With. Punctuation?"
s = re.sub(r'[^ws]','',s)
#without re
s = "string. With. Punctuation?"
s.translate(str.maketrans('', '', string.punctuation))
Comment

Removing punctuation in Python

import string 
from nltk.tokenize import word_tokenize
s =  set(string.punctuation)          # !"#$%&'()*+,-./:;<=>?@[]^_`{|}~
sentence = "Hey guys !, How are 'you' ?"
sentence = word_tokenize(sentence)
filtered_word = []
for i in sentence:
    if i not in s:
        filtered_word.append(i);
for word in filtered_word:
  print(word,end = " ")
Comment

remove punctuation python

import string 
sentence = "Hey guys !, How are 'you' ?"
no_punc_txt = ""
for char in sentence:
   if char not in string.punctuation:
       no_punc_txt = no_punc_txt + char
print(no_punc_txt);                 # Hey guys  How are you 
# or:
no_punc_txt = sentence.translate(sentence.maketrans('', '', string.punctuation))
print(no_punc_txt);                 # Hey guys  How are you 
Comment

Python remove punctuation from a string

# Python program to remove punctuation from a string

import string
text= 'Hello, W_orl$d#!'

# Using translate method
print(text.translate(str.maketrans('', '', string.punctuation)))
Comment

function to remove punctuation in python

import string

def remove_punctuation(text):
    '''a function for removing punctuation'''
    # replacing the punctuations with no space, 
    # which in effect deletes the punctuation marks 
    translator = str.maketrans('', '', string.punctuation)
    # return the text stripped of punctuation marks
    return text.translate(translator)
Comment

remove punctuation

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

my_str = "Hello!!!, he said ---and went."

# To take input from the user
# my_str = input("Enter a string: ")

# remove punctuation from the string
no_punct = ""
for char in my_str:
   if char not in punctuations:
       no_punct = no_punct + char

# display the unpunctuated string
print(no_punct)
Comment

Remove Punctuation from a String

import string
 
test_str = 'Gfg, is best: for ! Geeks ;'
 
test_str = test_str.translate
    (str.maketrans('', '', string.punctuation))
print(test_str)
Comment

PREVIOUS NEXT
Code Example
Python :: how to unimport a file python 
Python :: elbow plot for k means clustering 
Python :: change column order pandas 
Python :: install python package 
Python :: looping over lists in python 
Python :: python autoclick website 
Python :: init array in numpy 
Python :: swap two lists without using third variable python 
Python :: python capitalize 
Python :: how to run class.function from name python 
Python :: selecting rows with specific values in pandas 
Python :: django migrations 
Python :: double for in loop python 
Python :: search in django 
Python :: django prevent duplicate entries 
Python :: // in python 
Python :: print string in reverse order uing for loop python 
Python :: quicksort algorithm in python 
Python :: change value in tuple 
Python :: python order number list 
Python :: how to add pagination in discord.py 
Python :: Label enconding code with sklearn 
Python :: giving number of letter in python 
Python :: Exception in thread 
Python :: print() function in python 
Python :: time converting module 
Python :: summing all Odd Numbers from 1 to N 
Python :: how to add element to list python 
Python :: daraja mpesa 
Python :: numpy datatime object 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =