Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. import re, string
  2. def removePunctuation(text):
  3. """Removes punctuation, changes to lower case, and strips leading and trailing spaces.
  4.  
  5. Note:
  6. Only spaces, letters, and numbers should be retained. Other characters should be
  7. eliminated (e.g. it's becomes its). Leading and trailing spaces should be removed after
  8. punctuation is removed.
  9.  
  10. Args:
  11. text (str): A string.
  12.  
  13. Returns:
  14. str: The cleaned up string.
  15. """
  16. regex = re.compile('[%s]' % re.escape(string.punctuation))
  17. no_punctuation = regex.sub('', text)
  18. no_tralling_spaces = no_punctuation.strip()
  19. lower_case = no_tralling_spaces.lower()
  20. return lower_case
  21.  
  22. print removePunctuation('Hi, you!')
  23. print removePunctuation(' No under_score!')
  24. print removePunctuation(' * Remove punctuation then spaces * ')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement