Showing posts with label leading and trailing spaces. Show all posts
Showing posts with label leading and trailing spaces. Show all posts

Wednesday, June 17, 2015

Remove punctation from the text - Python


import re
import string
def removePunctuation(text):
   
    #converts to lowercase
    text1 = text.lower()
   
    #removes punctuation
    #punctation = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
    text2 =  re.sub('[%s]' % string.punctuation, ' ' ,text1)

    #removes leading and trailing spaces
    text3 = text2.strip()
    return text3


   

print removePunctuation("'The best investments today,'")
print removePunctuation("Misery in Paradise;")

Output:

the best investments today
misery in paradise