daily pastebin goal
3%
SHARE
TWEET

Untitled

a guest Oct 12th, 2017 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def my_handle(self):
  2.     sentence = ' hello  apple  '
  3.     sentence.strip()
  4.    
  5. sentence = ' hello  apple'
  6. sentence.strip()
  7. >>> 'hello  apple'
  8.    
  9. sentence = ' hello  apple'
  10. sentence.replace(" ", "")
  11. >>> 'helloapple'
  12.    
  13. sentence = ' hello  apple'
  14. " ".join(sentence.split())
  15. >>> 'hello apple'
  16.    
  17. sentence = sentence.replace(' ', '')
  18.    
  19. sentence = ''.join(sentence.split())
  20.    
  21. import re
  22. pattern = re.compile(r's+')
  23. sentence = re.sub(pattern, '', sentence)
  24.    
  25. sentence = sentence.strip()
  26.    
  27. sentence = re.sub(r"s+", "", sentence, flags=re.UNICODE)
  28.    
  29. import string
  30. ' hello  apple'.translate(None, string.whitespace)
  31.    
  32. >> "  foo bar   ".strip()
  33. "foo bar"
  34.    
  35. ' hello  ntapple'.translate( { ord(c):None for c in ' ntr' } )
  36.    
  37. import re    
  38. sentence = ' hello  apple'
  39. re.sub(' ','',sentence) #helloworld (remove all spaces)
  40. re.sub('  ',' ',sentence) #hello world (remove double spaces)
RAW Paste Data
Top